Python Regular Expressions
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.
This section focuses on the regular expression handlers commonly used in Python.
Re.match function
Re.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)
Function parameter Description:
Pattern-matched Regular expression
String to match.
Flags flags that govern how regular expressions are matched, such as case sensitivity, multiline matching, and so on. See also: regular expression modifier-optional flag
The match succeeds Re.match method returns a matching object, otherwise none is returned.
We can use the group (NUM) or groups () matching object function to get a matching expression.
Import Re Print (Re.match ('www'www.runoob.com'). span ()) # match at start position Print (Re.match ('com'www.runoob.com')) # Do not match at start position
ImportRe line="Cats is smarter than dogs"Matchobj= Re.match (r'(. *) is (. *?). *', line, re. m|Re. I)ifMatchobj:Print "Matchobj.group ():", Matchobj.group ()Print "Matchobj.group (1):", Matchobj.group (1) Print "Matchobj.group (2):", Matchobj.group (2)Else: Print "No match!!"
Results
Matchobj.group (): Cats is smarter than Dogsmatchobj.group (1): catsmatchobj.group (2): Smarter
Re.search method
Re.search scans the entire string and returns the first successful match.
function Syntax:
Re.search (Pattern, string, flags=0)
Function parameter Description:
Pattern-matched Regular expression
String to match.
The match succeeds Re.search method returns a matching object, otherwise none is returned.
We can use the group (NUM) or groups () matching object function to get a matching expression.
Group (num=0) matches the string of the entire expression, and group () can enter more than one group number at a time, in which case it returns a tuple that contains the corresponding values for those groups.
Import Re Print (Re.search ('www'www.runoob.com'). span ()) # match at start position Print (Re.search ('com'www.runoob.com'). span ()) # Do not match at start position
The above example runs the output as:
(0, 3) (11, 14)
The difference between Re.match and Re.search
Re.match matches only the beginning of the string, if the string does not begin to conform to the regular expression, the match fails, the function returns none, and Re.search matches the entire string until a match is found.
Retrieving and replacing
The Python re module provides re.sub to replace matches in a string.
Grammar:
Re.sub (Pattern, Repl, String, count=0, flags=0)
Parameters:
- Pattern: The modal string in the regular.
- REPL: The replacement string, or a function.
- String: The original string to be looked up for replacement.
- Count: The maximum number of times a pattern match is replaced, and the default of 0 means that all matches are replaced.
Re.compile function
The compile function compiles a regular expression and generates a regular expression (Pattern) object for use by the match () and search () functions.
The syntax format is:
Re.compile (pattern[, flags])
Parameters:
-
Pattern: A regular expression in the form of a string
-
Flags: optional, indicating a matching pattern, such as ignoring case, multiline mode, etc., with the following parameters:
- Re. I Ignore case
- Re. L represents a special character set \w, \w, \b, \b, \s, \s dependent on the current environment
- Re. M Multi-line mode
- Re. S is the . and any character including newline characters (. Line breaks are not included)
- Re. U represents special character set \w, \w, \b, \b, \d, \d, \s, \s dependent on Unicode character Property database
- Re. X to increase readability, ignore whitespace and comment after #
FindAll
Finds all substrings that match the regular expression in the string, returns a list, and returns an empty list if no match is found.
Note: match and search are matched once findall match all.
The syntax format is:
FindAll (string[, pos[, Endpos])
Parameters:
- String: strings to be matched.
- POS: Optional parameter that specifies the starting position of the string, which defaults to 0.
- Endpos: Optional parameter that specifies the end position of the string, which defaults to the length of the string.
Python Dafa good--python regular expressions