Hi Albert, On 16 August 2012 09:45, Albert-Jan Roskam <fo...@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. Walter _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor