re/sre woes with Python 2.0

John Hunter jdhunter at nitace.bsd.uchicago.edu
Wed Jul 17 13:41:48 EDT 2002


>>>>> "Jonathan" == Jonathan Epstein <Jonathan_Epstein at nih.gov> writes:

    Jonathan> Hi, Can someone please explain why the following line
    Jonathan> produces a regular-expression compilation error with
    Jonathan> Python 2.0, but not with Python 1.5?  Is this fixed in
    Jonathan> any later versions, and/or can you describe how to
    Jonathan> adjust the RE to make Python 2.0 happy?

    Jonathan> regexp2 = re.compile
    Jonathan> (r"(?s)STATE\s*\"([^\"]*)\"\s*(\w*)\s*(\w*)?\s*{(.*?)\n[
    Jonathan> \t]*}(?=\s*(?:$|STATE))")

Your problem can be boiled down to

regexp2 = re.compile(r"(\w*)?")

(\w*) zero or more word chars 
?     optional

Since zero or more is already optional in the sense of the '?', it
makes no sense to add it

regexp2 = re.compile(r"(\w*)")  #equivalent

I think what you want is

regexp2 = re.compile(r"(?s)STATE\s*\"([^\"]*)\"\s*(\w*)\s*(\w*)\s*{(.*?)\n[\t]*}(?=\s*(?:$|STATE))")


Take a look at

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&frame=right&th=a52889ac38a8a5be&seekm=87u2bsmhrp.fsf%40cartman.azz.net#link1

HTH,
John Hunter



More information about the Python-list mailing list