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

Skip to content

Commit 84df7fe

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

4 files changed

Lines changed: 67 additions & 12 deletions

File tree

Lib/re.py

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

Lib/sre_parse.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,25 @@ class Pattern:
6666
# master pattern object. keeps track of global attributes
6767
def __init__(self):
6868
self.flags = 0
69-
self.open = []
70-
self.groups = 1
7169
self.groupdict = {}
70+
self.subpatterns = [None] # group 0
71+
@property
72+
def groups(self):
73+
return len(self.subpatterns)
7274
def opengroup(self, name=None):
7375
gid = self.groups
74-
self.groups = gid + 1
76+
self.subpatterns.append(None)
7577
if name is not None:
7678
ogid = self.groupdict.get(name, None)
7779
if ogid is not None:
7880
raise error("redefinition of group name %s as group %d; "
7981
"was group %d" % (repr(name), gid, ogid))
8082
self.groupdict[name] = gid
81-
self.open.append(gid)
8283
return gid
83-
def closegroup(self, gid):
84-
self.open.remove(gid)
84+
def closegroup(self, gid, p):
85+
self.subpatterns[gid] = p
8586
def checkgroup(self, gid):
86-
return gid < self.groups and gid not in self.open
87+
return gid < self.groups and self.subpatterns[gid] is not None
8788

8889
class SubPattern:
8990
# a subpattern, in intermediate form
@@ -181,7 +182,21 @@ def getwidth(self):
181182
elif op in UNITCODES:
182183
lo = lo + 1
183184
hi = hi + 1
184-
elif op == SUCCESS:
185+
elif op is GROUPREF:
186+
i, j = self.pattern.subpatterns[av].getwidth()
187+
lo = lo + i
188+
hi = hi + j
189+
elif op is GROUPREF_EXISTS:
190+
i, j = av[1].getwidth()
191+
if av[2] is not None:
192+
l, h = av[2].getwidth()
193+
i = min(i, l)
194+
j = max(j, h)
195+
else:
196+
i = 0
197+
lo = lo + i
198+
hi = hi + j
199+
elif op is SUCCESS:
185200
break
186201
self.width = min(lo, MAXREPEAT - 1), min(hi, MAXREPEAT)
187202
return self.width
@@ -709,7 +724,7 @@ def _parse(source, state):
709724
if not sourcematch(")"):
710725
raise error("unbalanced parenthesis")
711726
if group is not None:
712-
state.closegroup(group)
727+
state.closegroup(group, p)
713728
subpatternappend((SUBPATTERN, (group, p)))
714729
else:
715730
while 1:

Lib/test/test_re.py

Lines changed: 37 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,42 @@ 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+
# Conditional group reference.
578+
self.assertTrue(re.match('(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
579+
self.assertIsNone(re.match('(?:(a)|(x))b(?=(?(2)c|x))c', 'abc'))
580+
self.assertTrue(re.match('(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
581+
self.assertIsNone(re.match('(?:(a)|(x))b(?=(?(1)b|x))c', 'abc'))
582+
self.assertTrue(re.match('(?:(a)|(x))b(?=(?(1)c|x))c', 'abc'))
583+
# Group used before defined.
584+
self.assertTrue(re.match('(a)b(?=(?(2)x|c))(c)', 'abc'))
585+
self.assertIsNone(re.match('(a)b(?=(?(2)b|x))(c)', 'abc'))
586+
self.assertTrue(re.match('(a)b(?=(?(1)c|x))(c)', 'abc'))
587+
588+
def test_lookbehind(self):
589+
self.assertTrue(re.match('ab(?<=b)c', 'abc'))
590+
self.assertIsNone(re.match('ab(?<=c)c', 'abc'))
591+
self.assertIsNone(re.match('ab(?<!b)c', 'abc'))
592+
self.assertTrue(re.match('ab(?<!c)c', 'abc'))
593+
# Group reference.
594+
self.assertTrue(re.match(r'(a)a(?<=\1)c', 'aac'))
595+
self.assertIsNone(re.match(r'(a)b(?<=\1)a', 'abaa'))
596+
self.assertIsNone(re.match(r'(a)a(?<!\1)c', 'aac'))
597+
self.assertTrue(re.match(r'(a)b(?<!\1)a', 'abaa'))
598+
# Conditional group reference.
599+
self.assertIsNone(re.match('(?:(a)|(x))b(?<=(?(2)x|c))c', 'abc'))
600+
self.assertIsNone(re.match('(?:(a)|(x))b(?<=(?(2)b|x))c', 'abc'))
601+
self.assertTrue(re.match('(?:(a)|(x))b(?<=(?(2)x|b))c', 'abc'))
602+
self.assertIsNone(re.match('(?:(a)|(x))b(?<=(?(1)c|x))c', 'abc'))
603+
self.assertTrue(re.match('(?:(a)|(x))b(?<=(?(1)b|x))c', 'abc'))
604+
# Group used before defined.
605+
self.assertIsNone(re.match('(a)b(?<=(?(2)x|c))(c)', 'abc'))
606+
self.assertIsNone(re.match('(a)b(?<=(?(2)b|x))(c)', 'abc'))
607+
self.assertIsNone(re.match('(a)b(?<=(?(1)c|x))(c)', 'abc'))
608+
self.assertTrue(re.match('(a)b(?<=(?(1)b|x))(c)', 'abc'))
609+
574610
def test_ignore_case(self):
575611
self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
576612
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
@@ -36,6 +36,9 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- Issues #814253, #9179: Group references and conditional group references now
40+
work in lookbehind assertions in regular expressions.
41+
3942
- Issue #22406: Fixed the uu_codec codec incorrectly ported to 3.x.
4043
Based on patch by Martin Panter.
4144

0 commit comments

Comments
 (0)