Home > Back-end >  multiple repeat at position 27
multiple repeat at position 27

Time:09-01

z = "hjff  sdjsd , 2020 - dsds, 67 089 dsds "

Year = re.search(r"(,\s(?:18|19|20|21)[0-9]{2} \s-)", z)

print (Year)
error: multiple repeat at position 27

I want to match the year by this format , 2020 - . It works on https://regex101.com/ but getting the error from my code. I tried the other solutions from the same error but didn't work for me.

CodePudding user response:

Remove the from the pattern:

z = "hjff  sdjsd , 2020 - dsds, 67 089 dsds "

Year = re.search(r"(,\s(?:18|19|20|21)[0-9]{2}\s-)", z)

print(Year)

Prints:

<re.Match object; span=(12, 20), match=', 2020 -'>

CodePudding user response:

If you switch that website to "Python" mode, you'll see the problem.

[0-9]{2} 

{2} means to match the previous expression twice. Adding a quantifier to that is a Perl-specific extension to the regex syntax which disables backtracking. It's pointless when applied to an exact match count, since you can't backtrack with only one option, so it seems to me this is likely a typo to begin with. The reason it works on regex101.com is because that site uses PCRE (Perl compatible regular expressions) by default, so the redundant possessive modifier is allowed.

  • Related