The problem of backslashes in regular expressions is not always understood before. It was finally clear today that the data had been consulted.
One of the most important is that the backslash defined in Python's own string is also an escape character, and the backslash in the regular expression is also an escape character, so a backslash in a regular expression involves a double-conversion problem.
How should the regular expression be written to match 1 backslashes in a string? "\ \" Is this OK? Try to know, re module thrown abnormal, because "\ \" is a backslash, for the regular expression parser, is an escape character, but nothing behind, the natural error, "\\\" Three is definitely not, try four "\\\\", perfect match.
What does it mean to write regular expressions in Python with the most used raw string, the native string? There is only one conversion, no string conversion , only in the regular expression inside the conversion, so match a backslash of the regular expression can be written like this, Re_str_patt = r "\ \", some people will think, later write Windows file path what is convenient, Oh direct Path = r "C:\myforder\xx" is done, yes, this sentence no problem, but if you write path = r "c:\myforder\xx\", direct error, why? Because the backslash does not act as an escape character, it has an effect on the quotation marks (including single quotes) behind it, so that the quotation mark is not treated as a termination of the string, because there are characters behind it, but it does not, so it will be an error. In fact, you can turn to the raw string inside to indicate the quotation mark what to do? , you can find that path = r "\\123\" xxx is OK, and that raw string is not a limitation? However, Raw is designed to support the regular expression, and in the regular backslash is the escape character, so it is not possible to appear at the end of the string, so it is recommended not to use raw in other places.
On the backslash problem of the Python Express expressions