[ACCEPTED]-Overlapping matches in Regex-overlap

Accepted answer
Score: 31

A possible solution could be to use a positive look behind:


It 7 would give you the end position of:

  1. *n***n**nn &nbsp;
  2. n*n***n**n &nbsp;
  3. nn*n***n**

As mentionned 6 by Timothy Khouri, a positive lookahead is more intuitive

I would prefer 5 to his proposition (?=nn)n the simpler form:


That 4 would reference the first position of the strings you 3 want and would capture the second n in group(2).

That is so because:

  • Any valid regular expression can be used inside the lookahead.
  • If it contains capturing parentheses, the backreferences will be saved.

So group(1) and 2 group(2) will capture whatever 'n' represents 1 (even if it is a complicated regex).

Score: 29

Using a lookahead with a capturing group 5 works, at the expense of making your regex 4 slower and more complicated. An alternative 3 solution is to tell the Regex.Match() method 2 where the next match attempt should begin. Try 1 this:

Regex regexObj = new Regex("nn");
Match matchObj = regexObj.Match(subjectString);
while (matchObj.Success) {
    matchObj = regexObj.Match(subjectString, matchObj.Index + 1); 
Score: 2

AFAIK, there is no pure regex way to do 12 that at once (ie. returning the three captures 11 you request without loop).

Now, you can find 10 a pattern once, and loop on the search starting 9 with offset (found position + 1). Should 8 combine regex use with simple code.

[EDIT] Great, I 7 am downvoted when I basically said what 6 Jan shown...
[EDIT 2] To be clear: Jan's 5 answer is better. Not more precise, but 4 certainly more detailed, it deserves to 3 be chosen. I just don't understand why mine 2 is downvoted, since I still see nothing 1 incorrect in it. Not a big deal, just annoying.

More Related questions