[Tutor] sneaky re.compile --is this a bug??

Hugo Arts hugo.yoshi at gmail.com
Thu Aug 16 11:43:49 CEST 2012


On Thu, Aug 16, 2012 at 11:23 AM, Walter Prins <wprins at gmail.com> wrote:

> Hi Albert,
>
> On 16 August 2012 09:45, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> >
> > Hi,
> >
> > Is it intended behavior that regular expression flags are ignored when
> > compiled regexes are used? In the code below, I intend to match path
> names
> > case-independently, but once the flags (in this case: no flags at all)
> have
> > been set in re.compile, subsequent flags in re.search are ignored (no pun
> > intended). This is sneaky! Is this because of the old Python version I am
> > using?
>
> I don't think so .  Firstly I think you're arguably using the compiled
> regex incorrectly.  The normal idiom for a compiled regex is:
> compiled_regex = re.compile(....)
>
> and then:
> compiled_regex.match(...)
> compiled_regex.search(...)
>
> Note, both the regular expression and flags to be used is "baked" into
> the compiled regex object.
>
> By contrast, you're calling re.search() and then passing a previously
> compiled regex (instead of a regex pattern as strictly speaking is
> required) to the re.search() method.  I suspect that what's happening
> is that re.search() is perhaps trying to be being helpful by seeing
> that it's not been given a regular expression but intead a compiled
> regex, and is then therefore relaying the re.search() request back to
> the compiled regex's search() method.  But, as mentioned, a compiled
> regex includes the flags it was compiled with, which may differ from
> the ones passed to re.search(), which is I think why you're seeing
> what you're seeing.  (Observe also the compile regex object's search()
> method does not accept a flags parameter.)  If you want to use
> different flags you must compile another copy of the regex expression
> if you want to use the regex in a compiled form.
>
> All of that said, you can actually inspect the flags applicable to a
> compiled regex by evaluating the "flags" attribute on the compiled
> regex object.  This will report a combination of the flags given to
> compile() and any flags specified inline inside the regular expression
> itself.  It may be worth enquiring on the Python development list
> whether the behaviour around your case of re.search( compiled_regex,
> flags) should be perhaps handled slightly differently if the flags
> specified do not match the already existing flags in the compiled
> regex, perhaps by raising an exception.  The principle of least
> surprise would seem to suggest that this might be better than silently
> giving you something else than asked for.
>
>
To add to this, in python 2.6.5 trying to do this raises an error:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> comp = re.compile('a')
>>> re.match(comp, 'A', re.I)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/re.py", line 137, in match
    return _compile(pattern, flags).match(string)
  File "/usr/lib/python2.6/re.py", line 238, in _compile
    raise ValueError('Cannot process flags argument with a compiled
pattern')
ValueError: Cannot process flags argument with a compiled pattern
>>>

Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120816/705a70ae/attachment-0001.html>


More information about the Tutor mailing list