escapes in regular expressions

Dennis Benzinger Dennis.Benzinger at gmx.net
Sun May 21 14:48:57 EDT 2006


James Thiele schrieb:
> I was helping a guy at work with regular expressions and found
> something I didn't expect:
> 
> 
>>>>re.match('\d', '7').group()
> 
> '7'

'\d' is not recognized as a escape sequence by Python and therefore it
is left unchanged <http://docs.python.org/ref/strings.html> in the
string which is passed to re.match.

> 
>>>>re.match('\\d', '7').group()
> 
> '7'
> [...]

This is the correct version. The first backslash escapes the second one
and this version will work even if a future version of Python recognizes
   \d as an escape sequence.


Dennis



More information about the Python-list mailing list