RMalke
RMalke

Reputation: 4094

Look Behind Regex

I'm starting with Regex (always used from the net the ones I needed)

I need something that given the input:

Input: AAABBBCCC
Index: 012345678

The regex matches would be:

The regex I have now is (A{2}|B{2}|C{2}). It is not my real problem, but I have different workings Regexes for the As, Bs and Cs.

I think that I should use some look behind operator but trying: ((A{2}|B{2}|C{2})$1) or (?<=(A{2}|B{2}|C{2})) won't work.

Here's an example.

Note: My problem is in c#, if that matters

Upvotes: 4

Views: 200

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

You do need lookaround but I'd use a positive lookahead assertion for that:

(?=(([ABC])\2))

Your match results will be in match.Groups(1) of each match object.

Explanation:

(?=       # Look ahead to check that the following matches:
 (        # Match and capture in group number 1:
  (       # Match and capture in group number 2:
   [ABC]  # Any letter A, B or C
  )       # End of capturing group 2
  \2      # Now match that same letter again.
 )        # End of group 1. It now contains AA, BB or CC
)         # End of lookahead assertion

A simpler solution:

(?=(AA|BB|CC))

Upvotes: 6

Related Questions