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