Regex anomaly

Andrew Durdin adurdin at gmail.com
Tue Jan 3 00:18:03 EST 2006


On 2 Jan 2006 21:00:53 -0800, mike.klaas at gmail.com <mike.klaas at gmail.com> wrote:
>
> Has anyone has issue with compiled re's vis-a-vis the re.I (ignore
> case) flag?  I can't make sense of this compiled re producing a
> different match when given the flag, odd both in it's difference from
> the uncompiled regex (as I thought the uncompiled api was a wrapper
> around a compile-and-execute block) and it's difference from the
> compiled version with no flag specified.  The match given is utter
> nonsense given the input re.

The re.compile and re.match methods take the flag parameter:

    compile( pattern[, flags])
    match( pattern, string[, flags])

But the regular expression object method takes different paramters:

    match( string[, pos[, endpos]])

It's not a little confusing that the parameters to re.match() and
re.compile().match() are so different, but that's the cause of what
you're seeing.

You need to do:

    reCompiled = re.compile(reStr, re.I)
    reCompiled.match(against).groups()

to get the behaviour you want.

Andrew



More information about the Python-list mailing list