Python quantifier alternative

I am trying to match all occurrences of a String Article , followed by a number (one or more digits), not followed by opening parentheses. In Sublime Text, I use the following regular expression:

 Article\s[0-9]++(?!\() 

to search for the next line:

 Article 29 Article 30(1) 

which does not comply with Article 30(1) (as I expect), but Article 29 and Article 1 .

When trying to do the same in Python (3) using

 import re article_list = re.findall(r'Article\s[0-9]++(?!\()', "Article 30(1)") 

I get the following error as I use a (nested) possessive quantifier that is not supported by the Python regular expression. Is there a way to map what I want (not) to fit in Python?

+5
source share
2 answers

Python re does not support possessive quantifiers. You can instead use the Python PyPi regex module , which supports this type of quantifier. Or use the following workarounds.

You need to either add a number to the list:

 Article\s[0-9]+(?![(0-9]) ^^^ 

Watch this demo version of regex .

Alternatively use a word boundary:

 Article\s[0-9]+\b(?!\() ^ 

Watch this demo version of regex .

+3
source

You can also emulate an atomic group (?>...) around what you want to map using a workaround (?=(...))\1 :

 (?=(Article\s[0-9]+))\1(?!\() 

(lookahead behaves naturally as an atomic group, all you need is capture and backlink)

+2
source

Source: https://habr.com/ru/post/1268705/


All Articles