Once again, the virus writers sent employees a Javascript script archive. There is a tricky gathering of some address from ordinary characters and symbols in acsii (example \x65 ).

As I study Python, I wanted to replace these ascii characters with regular ones directly in the text and then put them into the address. Trying to define ascii-characters through a regular expression came across such that I can not write a backslash in the expression. double backslash gives two slashes and not one as counted. Prompt to write a regular expression for a string of the form "\x65" .

    2 answers 2

    https://ideone.com/fGi24c

     import re s = input() match = re.search('\\\\x\d\d', s, re.IGNORECASE) print(match) 
    • Why do two backslashes fall into match? In your example, there is only one in the search string. - plmax
    • @plmax, like in the next answer already explained. - Qwertiy
    • I need to get a substring with one slash. - plmax
    • @plmax, so I got one. - Qwertiy pm
    • <_sre.SRE_Match object; span = (12, 16), match = '\\ x65'> in the output as in regular expressions, two slashes count for one? - plmax

    The backslash in regular expressions — since it has a special meaning in them — you need to write as a pair of backslashes - \\ .

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

    But the other is also possible - the best approach. Literals with the letter r immediately before the opening apostrophe or quotation mark understand Python literally, without interpreting them.

    As a result, you have 2 approaches, how to transfer the necessary 2 backslashes from Python to a 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"\\" .