Python elegant use of regular expressions ~ 2

Source: Internet
Author: User

Using regular expressions Those basic theories also say a lot about it now start to practice (don't know why especially like the word ...). The last lesson said that the regular expression is also a language, he was integrated into the python, and with the RE module for the regular expression provides an interface. The regular expression is written in C, so the efficiency is very high, we compile the regular expression into a pattern object, and use them to match, this is to improve efficiency, we will often use the "pattern", refers to the regular expression compiled regular expression. The regular expression is compiled into a schema object that has various methods for manipulating strings, such as finding pattern matching or performing string substitution.
>>> Import re>>> p = re.compile ('ab*') # A and One to the positive infinity of B┌──────────────────────────────────────────────────────────────┐│re.compile: (pattern, flags=0  )                               ││compile A regular expression pattern, returning a pattern ob││ject.                                                        │└──────────────────────────────────────────────────────────────┘
As can be seen from the above, Re.compile () can also accept the flags parameter for opening various special functions and grammatical changes.
1 >>> p = re.compile ('ab*', re. IGNORECASE)

The regular expression is passed as a string parameter to Re.compile (). Since the regular expression is not a core part of Python, there is no special syntax support for him, so the regular expression can only be represented as a string. (Some apps don't need regular expressions at all, so the Python community's small partners don't think it's necessary to include them in the Python core) instead, the RE module is simply included as a C extension in Python, like the socket module and the Zlib module

Because of the strong, and the function of many, so \ also have a lot of trouble place.

match characters Match phase
\select need a matching string
regular expression needs \ \ to represent \
\\\\select Unfortunately Python also needs to use \ \ to represent \
1*2*2  so it used four \

In short, in order to match the backslash character we need to use four backslashes in the string, which is used frequently in regular expressions, and so on. Scary!

If there is a problem, there is a near-the-way approach, which is to use the original string to represent the regular expression. (r ' xxx ' Do you Remember?)

/tr>
original string
ab* r" ab* "
r "\\select"
r "\w+\s+\1"
python also need to use \ \ to represent \  oh

Python elegant use of regular expressions ~ 2

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.