12

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?

user
  • 123
  • 1
  • 5

3 Answers3

9

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)

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
5

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

Just an additional information to the work-arounds:

From Python 3.11 on, the re module supports atomic grouping and possessive quantifiers, see https://docs.python.org/3.11/whatsnew/3.11.html#re and https://github.com/python/cpython/issues/34627.