Home > Enterprise >  What would be the Regex to match following 6 digit numbers?
What would be the Regex to match following 6 digit numbers?

Time:05-13

The numbers should be 6 digit and of the form 0x0x0x or x0x0x0, where x can be any digit from 1 to 9. Ex - 202020, 030303, 808080, etc.

I have this regex that matches numbers with alternative 0 and 1s, cannot make it work for the above use case

\b(?!\d*(\d)\1)[10] \b

CodePudding user response:

The easiest way is probably to capture the repeating part.
Then check if the back reference to group 1 is repeated.

\b(0[1-9]|[1-9]0)\1{2}\b

CodePudding user response:

If you don't mind repeating both "forms", an alternation is probably the most straightforward solution:

\b(?:([1-9])0\10\10|0([1-9])0\20\2)\b

https://regex101.com/r/R7fAbh/1

  • Related