Confused by slash/escape in regexp

Paul McGuire ptmcg at austin.rr.com
Sun Apr 11 19:18:46 EDT 2010


On Apr 11, 5:43 pm, andrew cooke <and... at acooke.org> wrote:
> 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

Here is your same session, but using raw string literals:

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from re import compile
>>> p1 = compile(r'a\x62c')
>>> p1.match(r'abc')
<_sre.SRE_Match object at 0x00A04BB8>
>>> p2 = compile(r'a\\x62c')
>>> p2.match(r'abc')
>>> p3 = compile(r'a\\\x62c')
>>> p3.match(r'a\\bc')
>>> p3.match(r'abc')
>>> p3.match(r'a\\\x62c')
>>>

So I would say the surprise isn't that case 3 didn't match, but that
case 2 matched.

Unless I just don't get what you were testing, not being an RE wiz.

-- Paul



More information about the Python-list mailing list