Note that there are some explanatory texts on larger screens.

plurals
  1. PORegex to capture an optional group in the middle of a block of input
    primarykey
    data
    text
    <p>I'm stuck on a RegEx problem that's seemingly very simple and yet I can't get it working.</p> <p>Suppose I have input like this:</p> <pre><code>Some text %interestingbit% lots of random text lots and lots more %anotherinterestingbit% Some text %interestingbit% lots of random text OPTIONAL_THING lots and lots more %anotherinterestingbit% Some text %interestingbit% lots of random text lots and lots more %anotherinterestingbit% </code></pre> <p>There are many repeating blocks in the input and in each block I want to capture some things that are always there (%interestingbit% and %anotherinterestingbit%), but there is also a bit of text that may or may not occur in-between them (OPTIONAL_THING) and I want to capture it if it's there.</p> <p>A RegEx like this matches only blocks with OPTIONAL_THING in it (and the named capture works):</p> <pre><code>%interestingbit%.+?((?&lt;OptionalCapture&gt;OPTIONAL_THING)).+?%anotherinterestingbit% </code></pre> <p>So it seems like it's just a matter of making the whole group optional, right? That's what I tried:</p> <pre><code>%interestingbit%.+?((?&lt;OptionalCapture&gt;OPTIONAL_THING))?.+?%anotherinterestingbit% </code></pre> <p>But I find that although this matches all 3 blocks the named capture (OptionalCapture) is empty in all of them! How do I get this to work?</p> <p>Note that there can be a lot of text within each block, including newlines, which is why I put in ".+?" rather than something more specific. I'm using .NET regular expressions, testing with The Regulator.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload