Skip to content Skip to sidebar Skip to footer

Regex Sub Is Throwing Some Error In Python

I am trying to replace a character with empty lines using re.sub in python but it is throwing some error import re string = '\asdfsdfsdf\dfd\f\df\df\d' re.sub('\\','',string) prin

Solution 1:

You need to define the input string as well as the pattern in raw string notation form.

>>>string = r"\asdfsdfsdf\dfd\f\df\df\d">>>print(string)
\asdfsdfsdf\dfd\f\df\df\d
>>>re.sub(r"\\","",string)
'asdfsdfsdfdfdfdfdfd'

If the input string is not defined as raw string, \a in the string would turn into an unicode character.

Post a Comment for "Regex Sub Is Throwing Some Error In Python"