How to do mutli line matches where some of the later matches are optional?
20:24 06 May 2026

I am trying to parse out a text document with various sections that have different headings. However, those headings don't always exist. If I use the following regex, the matching works fine:

Pattern:

(?One[\w\W]+)(?Two[\w\W]+)(?Three[\w\W]+)

Test case:

One
...
...
...
Two
...
...
...
Three
...
...
...

https://regex101.com/r/FOgBMg/1

But if either sections Two or Three are missing, then the entire match fails.

If I try to make sections Two or Three optional with ? then the first section gobbles them up.

(?One[\w\W]+)(?Two[\w\W]+)(?Three[\w\W]+)

How do I make it so that sections Two and Three are optional but not gobbled by the first section?

regex pcre