[pypy-commit] pypy default: [fixes issue 923] matching RegExp with optional zero-width assertion groups

brunogola noreply at buildbot.pypy.org
Fri Oct 28 09:34:11 CEST 2011


Author: Bruno Gola <brunogola at gmail.com>
Branch: 
Changeset: r48548:4c56b2a60b5a
Date: 2011-10-27 19:52 -0200
http://bitbucket.org/pypy/pypy/changeset/4c56b2a60b5a/

Log:	[fixes issue 923] matching RegExp with optional zero-width assertion
	groups

diff --git a/pypy/rlib/rsre/rsre_core.py b/pypy/rlib/rsre/rsre_core.py
--- a/pypy/rlib/rsre/rsre_core.py
+++ b/pypy/rlib/rsre/rsre_core.py
@@ -389,6 +389,9 @@
             # zero-width match protection
             min = ctx.pat(ppos+1)
             if self.num_pending >= min:
+                if ptr == ctx.match_end and ctx.match_marks:
+                    # matched marks inside a zero-width assertion
+                    marks = ctx.match_marks
                 while enum is not None and ptr == ctx.match_end:
                     enum = enum.move_to_next_result(ctx)
             #
diff --git a/pypy/rlib/rsre/test/test_re.py b/pypy/rlib/rsre/test/test_re.py
--- a/pypy/rlib/rsre/test/test_re.py
+++ b/pypy/rlib/rsre/test/test_re.py
@@ -226,6 +226,13 @@
                          (None, 'b', None))
         assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
 
+    def test_bug_923(self):
+        # Issue923: grouping inside optional lookahead problem
+        assert re.match(r'a(?=(b))?', "ab").groups() == ("b",)
+        assert re.match(r'(a(?=(b))?)', "ab").groups() == ('a', 'b')
+        assert re.match(r'(a)(?=(b))?', "ab").groups() == ('a', 'b')
+        assert re.match(r'(?P<g1>a)(?=(?P<g2>b))?', "ab").groupdict() == {'g1': 'a', 'g2': 'b'}
+
     def test_re_groupref_exists(self):
         assert re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups() == (
                          ('(', 'a'))


More information about the pypy-commit mailing list