Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Groups of Consecutive Numbers Regex Python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.{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?

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading