-->

Alternative to possessive quantifier in python

2020-06-14 08:08发布

问题:

I am trying to match all occurences of the String Article followed by a number (single or more digits) which are not followed by an opening parentheses. In Sublime Text, I am using the following regex:

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

to search the following String:

Article 29
Article 30(1)

which does not match Article 30(1) (as I expect it to) but Article 29 and Article 1.

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

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

I get an the following Error as I am using a (nested) possessive quantifier which is not supported by Python regex. Is there any way to match what I want it (not) to match in Python?

回答1:

Python re does not support possessive quantifiers. You may consider using Python PyPi regex module instead, that supports this type of quantifiers. Or use the following work-arounds.

You need to either add a digit to the lookahead:

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

See this regex demo.

Alternatively, use a word boundary:

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

See this regex demo.



回答2:

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

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

(a lookahead behaves naturally like an a atomic group, all you need is a capture and a backreference)