Match Two Sets of Different Consecutive Numbers Regex Python

Question:

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

Asked By: Leena

||

Answers:

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.

bdd(d)(?!1)(d)22(d)33b
  • b A word boundary to prevent a partial word match
  • dd Match 2 digits
  • (d)(?!1) Capture a single digit in group 1, and assert that it is not followed by the same
  • (d)22 Capture a single digit in group 2 and match 2 times the same digits after it
  • (d)33 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:

bdd(d)(?!1)(d)(?!dd2)22(d)33b

Regex demo

Answered By: The fourth bird
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.