Python (All) Re functions: Compile, match, search, FindAll

Source: Internet
Author: User

A regular expression is a special sequence of characters that can help you easily check whether a string matches a pattern.

Python has added the RE module since version 1.5, which provides a Perl-style regular expression pattern.

The RE module enables the Python language to have all the regular expression functionality.

The compile function generates a regular expression object based on a pattern string and an optional flag parameter. The object has a series of methods for regular expression matching and substitution.

The RE module also provides functions that are fully consistent with these methods, which use a pattern string as their first parameter.

Parameter description:

Function parameter Description:

Parameters Description
Pattern Matched regular Expressions
String The string to match.
Flags A flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.

Re.match function:

The match attempts to match a pattern from the starting position of the string, and if the match is not successful, match () returns none.

function syntax: re. Match(pattern, string, flags=0)

Match result: Re.match match succeeds returns an object, otherwise none is returned.

Use Group (num=0) or groups () to get matching results

Re.search function:

Scans the entire string and returns the first successful match.

function syntax:re. Search(pattern, string, flags=0)

Parameters above

Match result: Returns a matching object if the match succeeds, otherwise none is returned.

Use Group (num=0) or groups () to get the result of the match.

Re.findall function:

Scans the entire string and returns the matching content as a list .

function syntax: Re. Search(pattern, string, flags=0)

Python (All) Re functions: Compile, match, search, FindAll

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.