raw strings in regexps

Mike mike at nospam.com
Fri Dec 8 00:53:54 EST 2006


I've been having trouble with a regular expression, and I finally simplified 
things down to the point that (a) my example is very simple, and (b) I'm 
totally confused. There are those who would say (b) is normal, but that's 
another thread.

I finally simplified my problem down to this simple case:

   re.match(r'\\this', r'\\this')

Both the pattern and the string to match are identical raw strings, yet they 
don't match. What does match is this:

   re.match(r'\\\\this', r'\\this')

Below are outputs from two versions of Python on two different machines, 
with identical outputs, so it's probably not a compiler problem or a version 
bug.

What's going on here? Am I missing something obvious?

linux25> python
Python 2.2.3 (#1, Feb  2 2005, 12:22:48)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> print re.match(r'\\this', r'\\this')
None
>>> print re.match(r'\\\\this', r'\\this')
<_sre.SRE_Match object at 0x6bf0e0>
>>>

C:\Dev\python>python
ActivePython 2.4.2 Build 10 (ActiveState Corp.) based on
Python 2.4.2 (#67, Jan 17 2006, 15:36:03) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> print re.match(r'\\this', r'\\this')
None
>>> print re.match(r'\\\\this', r'\\this')
<_sre.SRE_Match object at 0x009DA058>
>>>

-- Mike --





More information about the Python-list mailing list