Confused by slash/escape in regexp

andrew cooke andrew at acooke.org
Sun Apr 11 18:43:27 EDT 2010


Is the third case here surprising to anyone else?  It doesn't make
sense to me...

Python 2.6.2 (r262:71600, Oct 24 2009, 03:15:21)
[GCC 4.4.1 [gcc-4_4-branch revision 150839]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from re import compile
>>> p1 = compile('a\x62c')
>>> p1.match('abc')
<_sre.SRE_Match object at 0x7f4e8f93d578>
>>> p2 = compile('a\\x62c')
>>> p2.match('abc')
<_sre.SRE_Match object at 0x7f4e8f93d920>
>>> p3 = compile('a\\\x62c')
>>> p3.match('a\\bc')
>>> p3.match('abc')
>>> p3.match('a\\\x62c')
>>>

Curious/confused,
Andrew



More information about the Python-list mailing list