On the backslash problem of the Python Express expressions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.