Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3369a5

Browse files
Issues #814253, #9179: Warnings now are raised when group references and
conditional group references are used in lookbehind assertions in regular expressions.
1 parent a1543cd commit a3369a5

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

Doc/library/re.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ The special characters are:
281281
assertion`. ``(?<=abc)def`` will find a match in ``abcdef``, since the
282282
lookbehind will back up 3 characters and check if the contained pattern matches.
283283
The contained pattern must only match strings of some fixed length, meaning that
284-
``abc`` or ``a|b`` are allowed, but ``a*`` and ``a{3,4}`` are not. Note that
284+
``abc`` or ``a|b`` are allowed, but ``a*`` and ``a{3,4}`` are not. Group
285+
references are not supported even if they match strings of some fixed length.
286+
Note that
285287
patterns which start with positive lookbehind assertions will not match at the
286288
beginning of the string being searched; you will most likely want to use the
287289
:func:`search` function rather than the :func:`match` function:
@@ -301,7 +303,8 @@ The special characters are:
301303
Matches if the current position in the string is not preceded by a match for
302304
``...``. This is called a :dfn:`negative lookbehind assertion`. Similar to
303305
positive lookbehind assertions, the contained pattern must only match strings of
304-
some fixed length. Patterns which start with negative lookbehind assertions may
306+
some fixed length and shouldn't contain group references.
307+
Patterns which start with negative lookbehind assertions may
305308
match at the beginning of the string being searched.
306309

307310
``(?(id/name)yes-pattern|no-pattern)``

Lib/sre_parse.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def __init__(self):
6969
self.open = []
7070
self.groups = 1
7171
self.groupdict = {}
72+
self.lookbehind = 0
73+
7274
def opengroup(self, name=None):
7375
gid = self.groups
7476
self.groups = gid + 1
@@ -352,6 +354,11 @@ def _escape(source, escape, state):
352354
if group < state.groups:
353355
if not state.checkgroup(group):
354356
raise error("cannot refer to open group")
357+
if state.lookbehind:
358+
import warnings
359+
warnings.warn('group references in lookbehind '
360+
'assertions are not supported',
361+
RuntimeWarning)
355362
return GROUPREF, group
356363
raise ValueError
357364
if len(escape) == 2:
@@ -630,6 +637,11 @@ def _parse(source, state):
630637
if gid is None:
631638
msg = "unknown group name: {0!r}".format(name)
632639
raise error(msg)
640+
if state.lookbehind:
641+
import warnings
642+
warnings.warn('group references in lookbehind '
643+
'assertions are not supported',
644+
RuntimeWarning)
633645
subpatternappend((GROUPREF, gid))
634646
continue
635647
else:
@@ -658,7 +670,10 @@ def _parse(source, state):
658670
raise error("syntax error")
659671
dir = -1 # lookbehind
660672
char = sourceget()
673+
state.lookbehind += 1
661674
p = _parse_sub(source, state)
675+
if dir < 0:
676+
state.lookbehind -= 1
662677
if not sourcematch(")"):
663678
raise error("unbalanced parenthesis")
664679
if char == "=":
@@ -689,6 +704,11 @@ def _parse(source, state):
689704
condgroup = int(condname)
690705
except ValueError:
691706
raise error("bad character in group name")
707+
if state.lookbehind:
708+
import warnings
709+
warnings.warn('group references in lookbehind '
710+
'assertions are not supported',
711+
RuntimeWarning)
692712
else:
693713
# flags
694714
if not source.next in FLAGS:

Lib/test/test_re.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def test_anyall(self):
557557
self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
558558
"a\n\nb")
559559

560-
def test_non_consuming(self):
560+
def test_lookahead(self):
561561
self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
562562
self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
563563
self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
@@ -571,6 +571,37 @@ def test_non_consuming(self):
571571
self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
572572
self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
573573

574+
# Group reference.
575+
self.assertTrue(re.match(r'(a)b(?=\1)a', 'aba'))
576+
self.assertIsNone(re.match(r'(a)b(?=\1)c', 'abac'))
577+
# Named group reference.
578+
self.assertTrue(re.match(r'(?P<g>a)b(?=(?P=g))a', 'aba'))
579+
self.assertIsNone(re.match(r'(?P<g>a)b(?=(?P=g))c', 'abac'))
580+
# Conditional group reference.
581+
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
582+
self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(2)c|x))c', 'abc'))
583+
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
584+
self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(1)b|x))c', 'abc'))
585+
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(1)c|x))c', 'abc'))
586+
# Group used before defined.
587+
self.assertTrue(re.match(r'(a)b(?=(?(2)x|c))(c)', 'abc'))
588+
self.assertIsNone(re.match(r'(a)b(?=(?(2)b|x))(c)', 'abc'))
589+
self.assertTrue(re.match(r'(a)b(?=(?(1)c|x))(c)', 'abc'))
590+
591+
def test_lookbehind(self):
592+
self.assertTrue(re.match(r'ab(?<=b)c', 'abc'))
593+
self.assertIsNone(re.match(r'ab(?<=c)c', 'abc'))
594+
self.assertIsNone(re.match(r'ab(?<!b)c', 'abc'))
595+
self.assertTrue(re.match(r'ab(?<!c)c', 'abc'))
596+
# Group reference.
597+
self.assertWarns(RuntimeWarning, re.compile, r'(a)a(?<=\1)c')
598+
# Named group reference.
599+
self.assertWarns(RuntimeWarning, re.compile, r'(?P<g>a)a(?<=(?P=g))c')
600+
# Conditional group reference.
601+
self.assertWarns(RuntimeWarning, re.compile, r'(a)b(?<=(?(1)b|x))c')
602+
# Group used before defined.
603+
self.assertWarns(RuntimeWarning, re.compile, r'(a)b(?<=(?(2)b|x))(c)')
604+
574605
def test_ignore_case(self):
575606
self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
576607
self.assertEqual(re.match(b"abc", b"ABC", re.I).group(0), b"ABC")

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issues #814253, #9179: Warnings now are raised when group references and
17+
conditional group references are used in lookbehind assertions in regular
18+
expressions.
19+
1620
- Issue #23215: Multibyte codecs with custom error handlers that ignores errors
1721
consumed too much memory and raised SystemError or MemoryError.
1822
Original patch by Aleksi Torhamo.

0 commit comments

Comments
 (0)