Alternative to possessive quantifier in python

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:

Articles[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'Articles[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?


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:

Articles[0-9]+(?![(0-9])
                    ^^^   

See this regex demo.

Alternatively, use a word boundary:

Articles[0-9]+b(?!()
                ^

See this regex demo.


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

(?=(Articles[0-9]+))1(?!()

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

链接地址: http://www.djcxy.com/p/76936.html

上一篇: 使用jQuery获取两个数组的比例

下一篇: python中所有格量词的替代