python – How to insert a backslash into a regular expression pattern?


Virus writers once again sent an archive with a Javascript script to employees. There, some address is cunningly collected from ordinary characters and characters in acsii (example \x65 ).

Since I'm learning Python, I wanted to replace these ascii characters with ordinary ones right in the text and then collect them into an address. Trying to determine ascii characters through a regular expression, I came across such that I can not write a backslash in the expression. double backslash gives two slashes and not one as expected. Tell me how to write a regular expression for a string like "\x65" .


The backslash in regular expressions – because it has a special meaning in them – must be written as a pair of backslashes – \\ .

But Python itself uses backslashes for special characters in regular string literals, such as the newline character \n . Therefore, in ordinary Python string literals, you need to write the backslash also as a pair of backslashes – \\ . As a result, for 2 backslashes you need to write 4 of them: "\\\\"

But possible is also another – better approach. Literals with an r immediately before an opening apostrophe or quotation mark are understood by Python literally, without interpreting them.

As a result, you have 2 approaches, how to transfer the necessary 2 backslashes from Python to the regular expression:

  1. Write 4 backslashes: "\\\\" – Python will interpret each pair of slashes as one, or
  2. Write 2 backslashes, but start the line with the letter r : r"\\" .
Scroll to Top