Commits

Bruno Gola committed 4c56b2a

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

  • Participants
  • Parent commits 1917da0

Comments (0)

Files changed (2)

File pypy/rlib/rsre/rsre_core.py

             # 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)
             #

File pypy/rlib/rsre/test/test_re.py

                          (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'))