raw strings in regexps

Gabriel Genellina gagsl-py at yahoo.com.ar
Fri Dec 8 02:34:33 EST 2006


At Friday 8/12/2006 02:53, Mike wrote:

>I finally simplified my problem down to this simple case:
>
>    re.match(r'\\this', r'\\this')
>
>Both the pattern and the string to match are identical raw strings, yet they
>don't match. What does match is this:
>
>    re.match(r'\\\\this', r'\\this')

Perhaps you can understand better with a simpler example without backslashes:

 >>> print re.match('(a)','(a)')
None
 >>> print re.match(r'\(a\)', '(a)')
<_sre.SRE_Match object at 0x00C5CDB0>

You have to quote metacharacters if you want to match them. The 
escape method is useful for this:

 >>> re.escape('(a)')
'\\(a\\)'


-- 
Gabriel Genellina
Softlab SRL 

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar



More information about the Python-list mailing list