Using regex in python -


I have the following problem: I want to run away from all the special characters in a dragon string.

  str = 'efex-x? K =; - 're.sub ("([^ a-zA-Z0- 9])", r' \\ 1 ', str) eFEx \\ 1x \\ 1k \\ 1 \\ 1 \\ 1' str = 'Efex-x? K =; - 're Shab ("([^ A-zA-Z0- 9]), 'R' '1', str) 'eFEx-x? K =; -' re.sub (" ([^ a-zA-Z0- 9 I can not seem to win here '1' indicates the special character and I am going to this special character before I want to add '\', but using its \ 1 means its special meaning is removed and \\ 1 also does not help.   

Use r '\\\ 1' . It is a backslash (escaped, hence \\ ) Verify by \ 1 .

For that it works, try:

  str = 'eFEx-x? K =; -' print again. ("([^ A-zA-Z0- 9] ) ", R '\\\ 1', str)  

It prints:

  efex \ -x \? K \ = \; \ -  

which I think you want. When interpreter output 'efex \\ - x \\? K \\ = \\ \\ - \' - Do not be confused, double backslash is because interpreter gives it output quote, unless you use print .


Comments