[Tutor] problem regarding re

Mats Wichmann mats at wichmann.us
Wed Jan 27 11:58:08 EST 2021


On 1/27/21 9:21 AM, Manprit Singh wrote:
> Dear Sir ,
> 
> Consider a problem given below
> 
> re.sub(r'\\', "", 'acd\m')
> 
> gives the output = acdm as re.sub will replace the "\" in "acd\m" with "" .
> 
> but when i am doing  re.sub(r'\\', "", 'acd\b') why is it not replacing "\"
> with "". why i am getting the answer as 'acd\x08' .  How can I get acdb ?
> kindly put some light on it.

Because the string you are operating on was not entered as a raw string, 
and thus was interpreted by Python. Since \b is a Python-recognized 
escape there's no backslash there when you go to substitute on it.  That 
didn't happen with the first string since \m has no special meaning to 
Python.




More information about the Tutor mailing list