Note that there are some explanatory texts on larger screens.

plurals
  1. PORegex: captures, groups, confusion
    text
    copied!<p>I can't seem to figure out captures + groups in Regex (.net).</p> <p>Let's say I have the following input string, where each letter is actually a placeholder for more complex regex expression (so simple character exclusion won't work):</p> <pre><code>CBDAEDBCEFBCD </code></pre> <p>Or, more generically, here is a string pattern written in 'regex':</p> <pre><code>(C|B|D)*A(E*)(D|B|C)*(E*)F(B|C|D)* </code></pre> <p>There will only be one A and one F. I need to capture as individual 'captures' (or matches or groups) all instances of B, C, D (which in my app are more complex groups) that occur after A and before F. I also need A and F. I don't need E. And I don't need the C,B,D before the A or the B,C,D after the F.</p> <p>I would expect the correct result to be:</p> <pre><code>Groups["start"] (1 capture) = A Groups["content"] (3 captures) Captures[0] = D Captures[1] = B Captures[2] = C Groups["end"] (1 capture) = F </code></pre> <p>I tried a few feeble attempts but none of them worked.</p> <p>Only "incorrectly" captures the last C before EF in the sample string above (as well as correctly start = A, end = F)</p> <pre><code>(?&lt;=(?&lt;start&gt;A)).+(?&lt;content&gt;B|C|D).+(?=(?&lt;end&gt;F)) </code></pre> <p>Same results as above (just added a + after (?B|C|D) )</p> <pre><code>(?&lt;=(?&lt;start&gt;A)).+(?&lt;content&gt;B|C|D)+.+(?=(?&lt;end&gt;F)) </code></pre> <p>Got rid of look-around stuff... same result as above</p> <pre><code>(?&lt;start&gt;A).+(?&lt;content&gt;B|C|D)+.+(?&lt;end&gt;F) </code></pre> <p>And then my good-for-nothing brain went on strike.</p> <p>So, what's the right way to approach this? Are look-arounds really needed for this or not?</p> <p>Thanks!</p>
 

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