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

Albert-Jan Roskam fomcl at yahoo.com
Thu Aug 16 10:45:31 CEST 2012


 
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?
 
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
>>> import re
>>> s = r"""DATA LIST         FILE = '\\besh1f\inkomen3\iis1\iistaak\produktie\dividend\base\j2010\bestand\D1305_01' /"""
>>> regex = "(file|name) ?= ?['\"]([^'\"]+)['\"]"
>>> m1 = re.search(regex, s, re.I)
>>> m1.group(2)
'\\\\besh1f\\inkomen3\\iis1\\iistaak\\produktie\\dividend\\base\\j2010\\bestand\\D1305_01'
>>> compiledRegex = re.compile(regex, s)    ## no ignorecase here
>>> m2 = re.search(compiledRegex, s, re.I)  ## re.I (ignorecase) is SILENTLY IGNORED
>>> m2 is None
True

Thanks in advance for your replies!

Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120816/38fdc390/attachment.html>


More information about the Tutor mailing list