8000 Issues #814253, #9179: Group references and conditional group referen… · python/cpython@4eea62f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4eea62f

Browse files
Issues #814253, #9179: Group references and conditional group references now
work in lookbehind assertions in regular expressions.
1 parent df80706 commit 4eea62f

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

Doc/library/re.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ The special characters are:
297297
>>> m.group(0)
298298
'egg'
299299

300+
.. versionchanged: 3.5
301+
Added support for group references of fixed length.
302+
300303
``(?<!...)``
301304
Matches if the current position in the string is not preceded by a match for
302305
``...``. This is called a :dfn:`negative lookbehind assertion`. Similar to

Lib/re.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,11 @@ def __init__(self, lexicon, flags=0):
351351
s = sre_parse.Pattern()
352352
s.flags = flags
353353
for phrase, action in lexicon:
354+
gid = s.opengroup()
354355
p.append(sre_parse.SubPattern(s, [
355-
(SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))),
356+
(SUBPATTERN, (gid, sre_parse.parse(phrase, flags))),
356357
]))
357-
s.groups = len(p)+1
358+
s.closegroup(gid, p[-1])
358359
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
359360
self.scanner = sre_compile.compile(p)
360361
def scan(self, string):

Lib/sre_parse.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ class Pattern:
6868
# master pattern object. keeps track of global attributes
6969
def __init__(self):
7070
self.flags = 0
71-
self.open = []
72-
self.groups = 1
7371
self.groupdict = {}
72+
self.subpatterns = [None] # group 0
73+
self.lookbehindgroups = None
74+
@property
75+
def groups(self):
76+
return len(self.subpatterns)
7477
def opengroup(self, name=None):
7578
gid = self.groups
76-
self.groups = gid + 1
79+
self.subpatterns.append(None)
7780
if self.groups > MAXGROUPS:
7881
raise error("groups number is too large")
7982
if name is not None:
@@ -82,12 +85,19 @@ def opengroup(self, name=None):
8285
raise error("redefinition of group name %r as group %d; "
8386
"was group %d" % (name, gid, ogid))
8487
self.groupdict[name] = gid
85-
self.open.append(gid)
8688
return gid
87-
def closegroup(self, gid):
88-
self.open.remove(gid)
89+
def closegroup(self, gid, p):
90+
self.subpatterns[gid] = p
8991
def checkgroup(self, gid):
90-
return gid < self.groups and gid not in self.open
92+
return gid < self.groups and self.subpatterns[gid] is not None
93+
94+
def checklookbehindgroup(self, gid, source):
95+
if self.lookbehindgroups is not None:
96+
if not self.checkgroup(gid):
97+
raise source.error('cannot refer to an open group')
98+
if gid >= self.lookbehindgroups:
99+
raise source.error('cannot refer to group defined in the same '
100+
'lookbehind subpattern')
91101

92102
class SubPattern:
93103
# a subpattern, in intermediate form
@@ -183,7 +193,21 @@ def getwidth(self):
183193
elif op in _UNITCODES:
184194
lo = lo + 1
185195
hi = hi + 1
186-
elif op == SUCCESS:
196+
elif op is GROUPREF:
197+
i, j = self.pattern.subpatterns[av].getwidth()
198+
lo = lo + i
199+
hi = hi + j
200+
elif op is GROUPREF_EXISTS:
201+
i, j = av[1].getwidth()
202+
if av[2] is not None:
203+
l, h = av[2].getwidth()
204+
i = min(i, l)
205+
j = max(j, h)
206+
else:
207+
i = 0
208+
lo = lo + i
209+
hi = hi + j
210+
elif op is SUCCESS:
187211
break
188212
self.width = min(lo, MAXREPEAT - 1), min(hi, MAXREPEAT)
189213
return self.width
@@ -379,6 +403,7 @@ def _escape(source, escape, state):
379403
if not state.checkgroup(group):
380404
raise source.error("cannot refer to open group",
381405
len(escape))
406+
state.checklookbehindgroup(group, source)
382407
return GROUPREF, group
383408
raise ValueError
384409
if len(escape) == 2:
@@ -641,6 +666,7 @@ def _parse(source, state):
641666
if gid is None:
642667
msg = "unknown group name: {0!r}".format(name)
643668
raise source.error(msg, len(name) + 1)
669+
state.checklookbehindgroup(gid, source)
644670
subpatternappend((GROUPREF, gid))
645671
continue
646672
else:
@@ -668,7 +694,13 @@ def _parse(source, state):
668694
if char is None or char not in "=!":
669695
raise source.error("syntax error")
670696
dir = -1 # lookbehind
697+
lookbehindgroups = state.lookbehindgroups
698+
if lookbehindgroups is None:
699+
state.lookbehindgroups = state.groups
671700
p = _parse_sub(source, state)
701+
if dir < 0:
702+
if lookbehindgroups is None:
703+
state.lookbehindgroups = None
672704
if not sourcematch(")"):
673705
raise source.error("unbalanced parenthesis")
674706
if char == "=":
@@ -701,6 +733,7 @@ def _parse(source, state):
701733
if condgroup >= MAXGROUPS:
702734
raise source.error("the group number is too large",
703735
len(condname) + 1)
736+
state.checklookbehindgroup(condgroup, source)
704737
elif char in FLAGS:
705738
# flags
706739
state.flags |= FLAGS[char]
@@ -726,7 +759,7 @@ def _parse(source, state):
726759
if not sourcematch(")"):
727760
raise source.error("unbalanced parenthesis")
728761
if group is not None:
729-
state.closegroup(group)
762+
state.closegroup(group, p)
730763
subpatternappend((SUBPATTERN, (group, p)))
731764
else:
732765
while True:

Lib/test/test_re.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ def test_anyall(self):
604604
self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
605605
"a\n\nb")
606606

607-
def test_non_consuming(self):
607+
def test_lookahead(self):
608608
self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
609609
self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
610610
self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
@@ -618,6 +618,46 @@ def test_non_consuming(self):
618618
self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
619619
self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
620620

621+
# Group reference.
622+
self.assertTrue(re.match(r'(a)b(?=\1)a', 'aba'))
623+
self.assertIsNone(re.match(r'(a)b(?=\1)c', 'abac'))
624+
# Conditional group reference.
625+
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
626+
self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(2)c|x))c', 'abc'))
627+
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
628+
self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(1)b|x))c', 'abc'))
629+
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(1)c|x))c', 'abc'))
630+
# Group used before defined.
631+
self.assertTrue(re.match(r'(a)b(?=(?(2)x|c))(c)', 'abc'))
632+
self.assertIsNone(re.match(r'(a)b(?=(?(2)b|x))(c)', 'abc'))
633+
self.assertTrue(re.match(r'(a)b(?=(?(1)c|x))(c)', 'abc'))
634+
635+
def test_lookbehind(self):
636+
self.assertTrue(re.match(r'ab(?<=b)c', 'abc'))
637+
self.assertIsNone(re.match(r'ab(?<=c)c', 'abc'))
638+
self.assertIsNone(re.match(r'ab(?<!b)c', 'abc'))
639+
self.assertTrue(re.match(r'ab(?<!c)c', 'abc'))
640+
# Group reference.
641+
self.assertTrue(re.match(r'(a)a(?<=\1)c', 'aac'))
642+
self.assertIsNone(re.match(r'(a)b(?<=\1)a', 'abaa'))
643+
self.assertIsNone(re.match(r'(a)a(?<!\1)c', 'aac'))
644+
self.assertTrue(re.match(r'(a)b(?<!\1)a', 'abaa'))
645+
# Conditional group reference.
646+
self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(2)x|c))c', 'abc'))
647+
self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(2)b|x))c', 'abc'))
648+
self.assertTrue(re.match(r'(?:(a)|(x))b(?<=(?(2)x|b))c', 'abc'))
649+
self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(1)c|x))c', 'abc'))
650+
self.assertTrue(re.match(r'(?:(a)|(x))b(?<=(?(1)b|x))c', 'abc'))
651+
# Group used before defined.
652+
self.assertRaises(re.error, re.compile, r'(a)b(?<=(?(2)b|x))(c)')
653+
self.assertIsNone(re.match(r'(a)b(?<=(?(1)c|x))(c)', 'abc'))
654+
self.assertTrue(re.match(r'(a)b(?<=(?(1)b|x))(c)', 'abc'))
655+
# Group defined in the same lookbehind pattern
656+
self.assertRaises(re.error, re.compile, r'(a)b(?<=(.)\2)(c)')
657+
self.assertRaises(re.error, re.compile, r'(a)b(?<=(?P<a>.)(?P=a))(c)')
658+
self.assertRaises(re.error, re.compile, r'(a)b(?<=(a)(?(2)b|x))(c)')
659+
self.assertRaises(re.error, re.compile, r'(a)b(?<=(.)(?<=\2))(c)')
660+
621661
def test_ignore_case(self):
622662
self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
623663
self.assertEqual(re.match(b"abc", b"ABC", re.I).group(0), b"ABC")

Misc/NEWS

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

16+
- Issues #814253, #9179: Group references and conditional group references now
17+
work in lookbehind assertions in regular expressions.
18+
1619
- Issue #23215: Multibyte codecs with custom error handlers that ignores errors
1720
consumed too much memory and raised SystemError or MemoryError.
1821
Original patch by Aleksi Torhamo.

0 commit comments

Comments
 (0)
0