multiple repeat at position 27

Question:

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.

Asked By: Siam Sadman

||

Answers:

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 -'>
Answered By: Andrej Kesely

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.

Answered By: Silvio Mayolo
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.