Home > Mobile >  Groups of Consecutive Numbers Regex Python
Groups of Consecutive Numbers Regex Python

Time:09-26

I am trying to match 4 couples of repeated numbers. This is the Pattern 5XXYYZZKK.

I have this sample 533992288

I was able to write this regex

.{1}(\d{2})(\d{2})(\d{2})(\d{2})

The problem with this regex is that it does not recognize the condition of 2 consecutive matching numbers XX YY ZZ KK

Can someone tell me what can I add/remove to make it work?

CodePudding user response:

Use backreferences:

5(\d)\1(\d)\2(\d)\3(\d)\4

Sample script:

inp = "533992288"
if re.search(r'^5(\d)\1(\d)\2(\d)\3(\d)\4$', inp):
    print("MATCH")
  • Related