Problem with RE matching backslash

Inyeol Lee inyeol.lee at
Tue Jan 27 19:55:55 CET 2004

On Tue, Jan 27, 2004 at 03:31:24PM +0000, Ladvánszky Károly wrote:
> Thanks for your quick and very helpful reply, Peter.
> What is still not clear to me is why  the search examples work and yield
> 'm'.
> Károly

Python and SRE have different convention for handling unrecognized
escape sequence, such as "\\m".

>From Section 2.4.1 String literals in Python 2.3.3 Reference Manual;
Unlike Standard  , all unrecognized escape sequences are left in the
string unchanged, i.e., the backslash is left in the string. (This
behavior is useful when debugging: if an escape sequence is
mistyped, the resulting output is more easily recognized as broken.)

>From Section 4.2.1 Regular Expression Syntax in Python 2.3.3 Lib Reference;
The special sequences consist of "\" and a character from the list
below. If the ordinary character is not on the list, then the resulting
RE will match the second character. For example, \$
matches the character "$". 

So, your 3rd example

    print'\\m', '\m').group(0)

is actually the same as

    print'm', '\\m').group(0)

, thus returns 'm'.

-Inyeol Lee

More information about the Python-list mailing list