Overlapping matches in Regex

I can't seem to find an answer to this problem, and I'm wondering if one exists. Simplified example:

Consider a string "nnnn", where I want to find all matches of "nn" - but also those that overlap with each other. So the regex would provide the following 3 matches:

  1. nnnn
  2. nnnn
  3. nnnn

I realize this is not exactly what regexes are meant for, but walking the string and parsing this manually seems like an awful lot of code, considering that in reality the matches would have to be done using a pattern, not a literal string.


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


It would give you the end position of:

  1. *n***n**nn  
  2. n*n***n**n  
  3. nn*n***n**

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

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


That would reference the first position of the strings you 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 group(2) will capture whatever 'n' represents (even if it is a complicated regex).

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

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

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

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

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

Need Your Help

Adding Values through local storage

javascript storage local

I'm trying to make a page where the user can input data into four different values in four different text boxes, and the values can be stored by using local storage, and then when the user refreshe...

JAX-RPC support in Netbeans 6.9.1

web-services plugins netbeans netbeans-6.9 jax-rpc

I am trying to generate new Web Service Client. When choosing Client Style to JAX-RPC Style I get a notification from Netbeans: "You must download JAX-RPC support plugin to create a JAX-RPC Client".