[ #456742 ] Failing test case for .*?

A.M. Kuchling amk at localhost.debian.org
Sat Nov 3 08:35:20 EST 2001


On Sat, 3 Nov 2001 04:50:38 -0600, Skip Montanaro <skip at pobox.com> wrote:
>Maybe there's some kind of operator precedence issue (the "?" binds more
>tightly than the "+") that means you have to wrap the first part of the
>pattern in a group.

+? is a non-greedy +, and it's not equivalent to  (...+)?.  

Here's a test program:

import sre
s = "a\nb\na1"

# Original, buggy pattern
p = sre.compile(r" [^\n]+? \d", sre.DEBUG | sre.VERBOSE)
m = p.search(s)
print (m and m.groups())

# Add a group
p = sre.compile(r" ([^\n]+?) \d", sre.DEBUG | sre.VERBOSE)
m = p.search(s)
print (m and m.groups())

When I run with the current CVS Python, two different results are produced,
even though the only difference is adding a pair of parentheses:

amk at glass:~/src/python$ ./python t.py
min_repeat 1 65535
  not_literal 10
in
  category category_digit
()
subpattern 1
  min_repeat 1 65535
    not_literal 10
in
  category category_digit
('a\nb\na',)
amk at glass:~/src/python$

There's definitely a bug here.

--amk



More information about the Python-list mailing list