backslash in regular expression problem

Darrell Gallion darrell at dorb.com
Sat Jun 17 02:39:34 EDT 2000


Try to avoid assigning to "string" that will cause problems when you import
string
Especially when your using "\" try to use raw strings. Note the r"xxxx"

>>> s=r"\END\\"
>>> print re.sub('[\\\]','',s)
END
>>> print re.sub(r'[\\]','',s)
END

--Darrell Gallion


----- Original Message -----
From: "Gordon McLeod" <Gordon.McLeod at wolfson.co.uk>
> import re
> string="\END\\"
> x=re.sub('[\]','',string)
> print x
>
> and expect x to be 'END' but I get:
>
> Traceback (innermost last):






More information about the Python-list mailing list