[issue1661] re.match ignores optional flags when receiving a compiled regex pattern

Brandon Corfman report at bugs.python.org
Wed Dec 19 16:40:04 CET 2007


New submission from Brandon Corfman:

Python's documentation for the re.match function is match(pattern,
string, [flags]) where pattern can be either a regex string or a
compiled regex object. If it's a compiled regex object, then supplying
an optional flag to re.match (in my case, re.IGNORECASE) doesn't work
and, more to the point, fails silently. I think this should throw an
exception if it's not going to work.

See
http://mysite.verizon.net/bcorfman/2007/12/something-i-hate-about-pythons-re.html
for a discussion.

A possible change (proposed by effbot) is below:
Index: re.py
===================================================================
--- re.py       (revision 52330)
+++ re.py       (working copy)
@@ -224,6 +224,7 @@
         return p
     pattern, flags = key
     if isinstance(pattern, _pattern_type):
+        assert not flags
         return pattern
     if not sre_compile.isstring(pattern):
         raise TypeError, "first argument must be string or compiled
pattern"

----------
components: Regular Expressions
messages: 58808
nosy: bcorfman
severity: normal
status: open
title: re.match ignores optional flags when receiving a compiled regex pattern
type: behavior
versions: Python 2.5

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1661>
__________________________________


More information about the Python-bugs-list mailing list