Python Regular expression backslash (/) trouble and trap _ regular expression

Source: Internet
Author: User
Tags in python
How do you write regular expressions to match 1 backslashes in a string? "\ \" is that all right? Try to know that the RE module throws an exception, because "\" is a backslash, for the regular expression parser, is an escape character, but the back of nothing, the natural error, "\\\" Three is certainly not, try four "\\\\", perfect match.
Copy Code code as follows:

Import re
Re_str_patt = "\\\\"
Reobj = Re.compile (Re_str_patt)
Str_test = "Abc\\cd\\hh"
Print Reobj.findall (str_test)


Output: [' \ \ ', ' \ \ ']

To understand this, the first conversion is the escape of the string itself, so "\\\\", in effect, represents two backslashes (two characters), and then passes in the regular expression parser, because the backslash is still the escape character, then the second conversion, the two backslash represents a backslash, So it can be matched against a backslash, so match two consecutive backslash, write regular expression must write 8 times "\", quite spectacular, to match/d+ (this in the regular expression inside 11 consecutive number of characters) how to write this string?
Copy Code code as follows:

Import re
Re_str_patt = "\\\\d\\+"
Print Re_str_patt
Reobj = Re.compile (Re_str_patt)
Print Reobj.findall ("\\d+")
Written Re_str_patt = "\\\\d\+" is also OK, because \+ for strings, there is no escape meaning, so it is a backslash.

The most common way to write regular expressions in Python is raw strings, native strings, what do you mean? It's just one shift, there is no string conversion, only within the regular expression of the conversion, so that a regular expression matching a backslash can be written in this way, Re_str_patt = R "\", some people will think, later write the file path of Windows what convenient, hehe direct path = R "C:\myforder\xx" buttoned up, yes, this sentence is no problem, but if you write path = r "C:\myforder\xx\", the direct error, why? Because the backslash is not an escape character, it has an effect on the quotation marks that follow it, including the single quotation mark, so that the quotation mark is not considered to be terminated by the string, and there are characters behind it, but there is no actual, and therefore an error. In fact, you can turn to the raw string inside to indicate what to do? , you can find that the path = r "\\123\" xxx is OK, then the raw string is not a limitation? Raw, however, is used to support regular expressions at the beginning of the design, while in the regular the backslash is an escape character, so it is not possible to appear at the end of the string, so it is not advisable to use raw in other places.

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.