Regex to capture 2 consecutive digits, but don't capture if 3 or more

JGFMK :

For example. If I have 1234X03.04

I want to capture the 03 and 04 But I don't want 12, 23, 34

I understand:

  • negative lookahead: (?!...)
  • negative lookbehind: (?<!...)

But, I don't know how to combine that into a single regex. Can someone help me out? Cheers.

MonkeyZeus :

You can use:

(?<!\d)\d{2}(?!\d)
  • (?<!\d) - prior char is not a digit
  • \d{2} - exactly two consecutive digits
  • (?!\d) - next char is not a digit

Here's a demo of the results in PHP. I think PHP's regex is close to Java's.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11291&siteId=1