Home > Software design >  Match Two Sets of Different Consecutive Numbers Regex Python
Match Two Sets of Different Consecutive Numbers Regex Python

Time:10-02

I am classifying a list of vanity phone numbers based on their patterns using regex.

I would like to capture this pattern 5ABXXXYYY

Sample 534666999

I wrote the below regex that captures XXXYYY.

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

I want to add a condition to assert the B is not the same number as X.

Desired output will match the given pattern exactly and replace it with the word silver.

S_2 = 534666999
S_2_pattern = re.sub(r"(\d)\2{2}(\d)\3{2}", "Silver", str(S_2))
print(S_2_pattern)
Silver

Thanks

CodePudding user response:

If you want to match 9 digits, and the 3rd digit should not be the same as the 4th, you can add another capture group for the 3rd digit and all the group numbers after are incremented by 1.

\b\d\d(\d)(?!\1)(\d)\2\2(\d)\3\3\b
  • \b A word boundary to prevent a partial word match
  • \d\d Match 2 digits
  • (\d)(?!\1) Capture a single digit in group 1, and assert that it is not followed by the same
  • (\d)\2\2 Capture a single digit in group 2 and match 2 times the same digits after it
  • (\d)\3\3 Capture a single digit in group 3 and match 2 times the same digits after it
  • \b A word boundary

Regex demo

If the first 3 digits in group 2 should also be different from the last 3 digits in group 3:

\b\d\d(\d)(?!\1)(\d)(?!\d\d\2)\2\2(\d)\3\3\b

Regex demo

  • Related