raw strings in regexps

Carl Banks pavlovevidence at gmail.com
Fri Dec 8 01:12: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.

In the pattern, the first backslash escapes the second; thus the two
backslashes in the pattern match one backslash in the string.  Another
example:

re.match(r"\[",r"\[") => False
re.match(r"\[",r"[") => True


Carl Banks




More information about the Python-list mailing list