The regular expression of Python

Source: Internet
Author: User

First, regular expression

First, we need to get a sense of what a regular expression is, and simply say "regular expression" is an "expression", more precisely defined as: "A concise way to implement a" set of strings "expression.

The ultimate goal is to achieve the "one line wins the thousand words" effect.

Second, the key to realize the regular expression

(1) operator (2) native string (3) Re library (4) Regular means object compilation

Third, detailed Jiu Zheng that the key element

  3.1 operator

We want to implement the expression of a set of strings using a simple expression, which requires an understanding of the operator first. Let's go back and look at the actual construction of the regular expression, we'll look at an example, as shown in the example below.

>>>> P(Y| YT| Yth| Ytho)?  N

From the example of the above regular expression, we can see that the construction of a regular expression consists of two parts: "string" + " operator ".

Therefore, in the process of learning regular expressions, we need to learn and understand the actual meaning of this operator and common instance scenarios. The specific meaning of the regular "operator" is simply collected as follows.

3.2 Native string 

Why introduce and learn native strings? This is mainly because of the standard library re Library of regular expressions in Python, which usually uses native strings to write various regular expressions, so we need to understand.

In fact, the so-called "native string" is mainly for our common "string", we just need to remember simple: the original string is not the "escape character (\)" String. Because this "\" is often met in regular operators, such as: \d,

\w ' and this backslash (\) in ordinary characters, refers to the transfer character, has a special meaning, will be some of the outstanding symbol symbols escaped as ordinary symbols. And in regular expressions to avoid unnecessary complex and wrong operations, it has a characteristic meaning

The "escaped meaning" of the string is erased.

The implementation of the specific native string can be seen in the following example.

native String implementation format:>>>> R ' text ' Practical Example:>>>> R ' [1-9\d{5}] ' >>>> R ' \d{3}-\d{8|\d{4}-\d{7}} '

 3.3 Re Library

 (1) Re Library brief introduction

The RE library is a standard library of Python's default, no need to install separately, directly using import to use the normal, the operation is as follows.

>>>>import RE

  

(2) Re Library main function function

For the 6 main functions of the RE library, stick to the collection listed in the table below.

 3.4 Regular Expression Object compilation

Regex = Re.compile (R ' text ')

  

The regular expression of Python

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.