Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding backslashes without escaping [duplicate]

I need to escape a & (ampersand) character in a string. The problem is whenever I string = string.replace ('&', '\&') the result is '\\&'. An extra backslash is added to escape the original backslash. How do I remove this extra backslash?

like image 334
Dr. Johnson Avatar asked Feb 01 '10 19:02

Dr. Johnson


People also ask

Why do some backslashes need to be escaped?

Answer. When a string is double quoted, it is processed by the compiler and again at run-time. Since a backslash (\) is removed whenever the string is processed, the double-quoted string needs double backslashes so that there is one left in the string at run time to escape a "special character".

Does backslash need to be escaped?

Control character But escape characters used in programming (such as the backslash, "\") are graphic, hence are not control characters. Conversely most (but not all) of the ASCII "control characters" have some control function in isolation, therefore they are not escape characters.

Why is Python adding backslashes to string?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

How do you add a backslash to a list in Python?

In short, to match a literal backslash, one has to write '\\\\' as the RE string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal.


1 Answers

The result '\\&' is only displayed - actually the string is \&:

>>> str = '&' >>> new_str = str.replace('&', '\&') >>> new_str '\\&' >>> print new_str \& 

Try it in a shell.

like image 97
Emil Ivanov Avatar answered Oct 09 '22 04:10

Emil Ivanov