Python core programming-Regular Expression Learning notes (2)

Source: Internet
Author: User

The Python language is the ability to support regular expressions through the RE module.

The RE module supports:

(1) Multiple threads share the same compiled regular expression object.

(2) support named subgroups.

Common functions of the RE module

1.match () function and the use of the search () function.

Difference: The match function matches the pattern from the starting part of the string.

The search function matches the first occurrence of a given pattern in any location.

Example:

The use of the 2.group () function and the groups () function.

Difference: the group () function returns the entire matching object or returns a specific subgroup.

The groups () function returns a tuple that contains subgroups.

Example:

The use of the 3.findall () and Finditer () functions.

FindAll () Returns a list of all occurrences of a regular expression pattern in a query string that are not duplicates.

Example:

The Finditer () function is similar but more memory-efficient. The return is an iterator.

Example:

Results:

The use of the 4.sub () and SUBN () functions.

Two functions are used to implement the search substitution function.

Difference: Sub () returns only the replaced string. Instead, SUBN () returns a tuple that consists of a replacement string and a total number of replacements.

Example:

Python core programming-Regular Expression Learning notes (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.