Regex Question

Bill Mill bill.mill at gmail.com
Wed Jan 10 11:21:15 EST 2007


Hello all,

I've got a test script:

==== start python code =====

tests2 = ["item1: alpha; item2: beta. item3 - gamma--",
"item1: alpha; item3 - gamma--"]

def test_re(regex):
    r = re.compile(regex, re.MULTILINE)
    for test in tests2:
        res = r.search(test)
        if res:
            print res.groups()
        else:
            print "Failed"

==== end python code ====

And a simple question:

Why does the first regex that follows successfully grab "beta", while
the second one doesn't?

In [131]: test_re(r"(?:item2: (.*?)\.)")
('beta',)
Failed

In [132]: test_re(r"(?:item2: (.*?)\.)?")
(None,)
(None,)

Shouldn't the '?' greedily grab the group match?

Thanks
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list