1

I'm new to regex and just want to know if its possible to find "overlaping" groups in the matches.

assume the following string:

20122 0029431 7094 0111 5890

I want now all matches which are: 4number+space+4number+space+4number

What I tried is this: [0-9]{4}[\s][0-9]{4}[\s][0-9]{4}

But this just gave me: 9431 7094 0111

What I want are these matches:

  • 9431 7094 0111
  • 7094 0111 5890

is this possible with regex?

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
blindmeis
  • 22,175
  • 7
  • 55
  • 74

1 Answers1

3

Yes, if you use lookahead assertions in combination with capturing groups:

Regex regexObj = new Regex(@"(?=(\d{4}\s\d{4}\s\d{4}))");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Groups[1].Value);
    matchResult = matchResult.NextMatch();
} 
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561