-1

I am trying to match the pattern "4 or more consecutive repeated digits". I know that the regex expression "(\d)\1{3}" works for this purpose as tested in https://regexr.com/. However the following code is not generating pattern match for any of the formulations:

import re

test_number = "5133336789123456"

pattern_1 = re.compile("(\d)\1{3}")
pattern_2 = re.compile("(\d)(\1{3})")
pattern_3 = re.compile(r"(\d)\1{3}")
pattern_4 = re.compile(r"(\d)(\1{3})")

print(pattern_1.match(test_number))
#None
print(pattern_2.match(test_number))
#None
print(pattern_3.match(test_number))
#None
print(pattern_4.match(test_number))
#None

What piece of this valid regex expression does not translate for Python regex?

ThiagoSC
  • 69
  • 1
  • 9
  • Use a raw string so that `\1` will be treated literally. – Barmar Aug 25 '23 at 00:15
  • 1
    @Barmar OP did use raw strings. The problem is that they used [`.match()` instead of `.search()`](https://stackoverflow.com/q/180986). – InSync Aug 25 '23 at 01:39
  • @InSync The first two are non-raw. Didn't notice they switched to raw in the last 2. – Barmar Aug 25 '23 at 01:56

0 Answers0