Regex: Matching a block with a pattern A until it hits another pattern B while capturing everything including the repeats of the pattern A?

  javascript, python, regex

The title may not correctly depict my question in mind, here is the detailed explanation below:

I have a testbank that includes a bunch of questions and answers. Questions start with numbers d+ followed by a dot ., followed by a question body .*. Full question pattern is d+.s+(.*). I only need the question body.

Question follows by a list of answers that each start with a letter and a closing bracket (ex=> A)) followed by the answer body. Here is the pattern for the question: w)s+?(.*)

Here is the sample text:

39. Lorem ipsum dolor sit amet consectetur adipisicing elit?
A) Officia quaerat
B) dignissimos mollitia 
C) ratione dolore dolores quam 
D) voluptatibus voluptate 
40. Lorem ipsum dolor sit amet consectetur adipisicing elit. Officia quaerat dignissimos mollitia ratione?
Officia quaerat dignissimos mollitia ratione?
1. question body text    2. question body text    3. question body text    4. question body text    5. question body text   6. question body text
A) Eaque, natus accusantium
B) Odio eum maxime voluptatibus
C) Est quos iure mollitia quas omnis
D) Expedita, eum cupiditate
41. Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quas, laboriosam 
A) consectetur molestias tenetur
B) debitis ex eos dolor tenetur
C) Sunt maiores voluptatibus
D) consectetur adipisicing elit

The issue is my question pattern does not work with question 40, which has a multiline block of text as one question. How do I properly assert that my regex captures everything from the number 40 40. until it reaches A)?

Here is the caveat: there is another matching pattern inside the question body:
1. question body text 2. question body text 3. question body text 4. question body text 5. question body text 6. question body text. Currently this section is captured as a separate question.

Here is the link to the same test string at regex101.

How would you go about this? Any help is appreciated.

Source: Ask Javascript Questions

LEAVE A COMMENT