Regular expression confusion

Fredrik Lundh fredrik at pythonware.com
Sun Sep 24 07:41:01 EDT 2006


York wrote:

> I have two backslash - a. and I want to replace them with one backslash, 
> but I failed:
> 
>  >>> import re
>  >>> a = '\\\\'
>  >>> re.sub(r'\\\\', '\\', '\\\\')

John has already sorted the RE-specific part of the problem, but it's 
also worth noting that using the RE engine for literal strings is over-
kill; an ordinary replace is easier to use and faster:

     >>> a = "\\\\"
     >>> a.replace("\\\\", "\\")
     '\\'

</F>




More information about the Python-list mailing list