Alternative to possessive quantifier in python

ε祈祈猫儿з 提交于 2019-12-12 07:59:27

问题


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)



来源:https://stackoverflow.com/questions/44458122/alternative-to-possessive-quantifier-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!