Problem with RE matching backslash

Peter Otten __peter__ at web.de
Tue Jan 27 18:56:13 CET 2004


Jeff Epler wrote:

>> What is still not clear to me is why  the search examples work and yield
>> 'm'.
> 
> Because the first pattern, which you wrote as "\\m", will match a lone
> 'm'.  So 'match' fails, because m is not in the first position.  But
> 'search' succeeds at index 1.
> 
> Jeff

This was my first guess, but it took me a while do find it documented. 
I will reproduce the tiny relevant paragraph from 

http://www.python.org/doc/current/lib/re-syntax.html

"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 "$". "
[description of characters with special meaning omitted]

I don't know if this list of characters is likely to grow, but I would
definitely prefer a "bogus escape" exception:

>>> re.compile("\\m")
<_sre.SRE_Pattern object at 0x40289660>

versus

>>> re.compile("\\1")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.3/sre.py", line 179, in compile
    return _compile(pattern, flags)
  File "/usr/local/lib/python2.3/sre.py", line 229, in _compile
    raise error, v # invalid expression
sre_constants.error: bogus escape: '\\1'


Peter



More information about the Python-list mailing list