regex to match question sentences in long text

I have a long text in form of a string.

This text includes a lot of questions that are at the same time the headers of sections.

These headers always start with a number+dot+whitespace character combination and end with a question mark, I am trying to extract these strings.

This is what I’ve got so far: longString.match(/d.s+[a-zA-Z]+s\?/g).
Sure enough this doesn’t work.

Stack Overflow Asked by Duke Wellington on December 29, 2020

1 Answers

One Answer

In your example you use [a-zA-Z]+, but you might extend that to matching 1 or more word characters using w+

This part at the end of the pattern s\? matches an expected whitespace char followed by an optional backslash.

To match multiple words, you can optionally repeat the pattern to match a word preceded by 1 or more whitespace characters.

You one option is to use



  • d. Match a single digit (for 1 or digits use d+)
  • s+w+ Match a . and 1+ whitspace chars and 1+ word chars
  • (?:s+w+)* Optionally repeat 1+ whitspace chars and 1+ word chars
  • s*? Match 0+ whitespace chars and a question mark.

Regex demo

A broader match might be matching at least a single time any char except a question mark or whitespace char after the digit, dot and whitespace:


Regex demo

Answered by The fourth bird on December 29, 2020

Add your own answers!

Related Questions

R mutate multiple columns with ifelse

1  Asked on December 7, 2021 by qnp1521


How can I show the code of my program in the output in python?

2  Asked on December 7, 2021 by ayaan-singh-saundh


Can’t unzip file with php

3  Asked on December 7, 2021 by ciro-garca


why isn’t my local variable overwriting the global?

4  Asked on December 7, 2021 by pillow-study


Variable not assigned even though I assigned it (Android Java)

1  Asked on December 7, 2021 by ictwinner


Function to find string with only one emoji

1  Asked on December 7, 2021 by user1199595


How to group buttons on Google Closure editor?

1  Asked on December 7, 2021 by frenchcooc


Mark non db fields @Transient vs extent JPA Entity?

1  Asked on December 7, 2021 by bluelurker


Ask a Question

Get help from others!

© 2022 All rights reserved.