raw strings in regexps

Fredrik Lundh fredrik at pythonware.com
Fri Dec 8 01:13:53 EST 2006


Mike wrote:

> I've been having trouble with a regular expression, and I finally simplified 
> things down to the point that (a) my example is very simple, and (b) I'm 
> totally confused. There are those who would say (b) is normal, but that's 
> another thread.
> 
> 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.

the regular expression engine matches a pattern against a string, not a 
string against a string.  in a pattern, "\\" matches a single backslash. 
  in your raw string, you have *two* backslashes.  try this instead:

     re.match(r'\\this', '\\this')

</F>




More information about the Python-list mailing list