Python regular expressions
A regular expression is a special sequence of characters that can help you easily check whether a string matches a certain pattern.
Python has added the re module since version 1.5, which provides Perl-style regular expression patterns. The
re module brings full regular expression capabilities to the Python language.
The compile function generates a regular expression object based on a pattern string and optional flag parameters. This object has a series of methods for regular expression matching and replacement. The
re module also provides functions that function exactly like these methods. These functions take a pattern string as their first argument.
This chapter mainly introduces the commonly used regular expression processing functions in Python.
re.match function
re.match attempts to match a pattern from the starting position of the string. If the matching is not successful from the starting position, match() returns none.
Function syntax:
re.match(pattern, string, flags=0)
Function parameter description:
Parameter | Description |
---|---|
pattern | Matching regular expression |
string | The string to match. |
flags | Flag bit is used to control the matching method of regular expressions, such as: whether to be case-sensitive, multi-line matching, etc. |
#The re.match method returns a matching object if the match is successful, otherwise it returns None.
We can use group(num) or groups() matching object function to get the matching expression.
##group(num=0)Match The string of the entire expression, group() can enter multiple group numbers at once, in which case it will return a tuple containing the values corresponding to those groups. groups()Returns a tuple containing all group strings, from 1 to the contained group number.Match object method | Description |
---|---|
#!/usr/bin/python # -*- coding: UTF-8 -*- import re print(re.match('www', 'www.php.cn').span()) # 在起始位置匹配 print(re.match('com', 'www.php.cn')) # 不在起始位置匹配The output result of the above instance is:
(0, 3) NoneInstance 2:
#!/usr/bin/python import re line = "Cats are smarter than dogs" matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) if matchObj: print "matchObj.group() : ", matchObj.group() print "matchObj.group(1) : ", matchObj.group(1) print "matchObj.group(2) : ", matchObj.group(2) else: print "No match!!"Above The instance execution results are as follows:
matchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter
re.search methodre.search scans the entire string and returns the first successful match. Function syntax:
re.search(pattern, string, flags=0)Function parameter description:##Parameterpatternstringflags
The re.search method returns a matching object if the match is successful, otherwise it returns None.
We can use group(num) or groups() matching object function to get the matching expression.
Description | |
---|---|
Matching regular expression | |
The string to match. | |
Flag bit is used to control the matching method of regular expressions, such as: whether to be case-sensitive, multi-line matching, etc. |
Match object method | Description |
---|---|
#!/usr/bin/python # -*- coding: UTF-8 -*- import re print(re.search('www', 'www.php.cn').span()) # 在起始位置匹配 print(re.search('com', 'www.php.cn').span()) # 不在起始位置匹配The output result of the above instance is:
(0, 3) (11, 14)Instance 2:
#!/usr/bin/python import re line = "Cats are smarter than dogs"; searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I) if searchObj: print "searchObj.group() : ", searchObj.group() print "searchObj.group(1) : ", searchObj.group(1) print "searchObj.group(2) : ", searchObj.group(2) else: print "Nothing found!!"The execution result of the above instance As follows:
searchObj.group() : Cats are smarter than dogs searchObj.group(1) : Cats searchObj.group(2) : smarter
The difference between re.match and re.searchre.match only matches the beginning of the string. If the beginning of the string does not match the regular expression, the match fails and the function returns None; whereas re.search matches the entire string until a match is found. Example:
#!/usr/bin/python import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj: print "match --> matchObj.group() : ", matchObj.group() else: print "No match!!" matchObj = re.search( r'dogs', line, re.M|re.I) if matchObj: print "search --> matchObj.group() : ", matchObj.group() else: print "No match!!"The results of the above example are as follows:
No match!! search --> matchObj.group() : dogs
Retrieval and replacementPython's re module provides re.sub for replacing strings. Matches. Syntax:
re.sub(pattern, repl, string, max=0)The returned string is replaced with the leftmost non-repeating match of RE in the string. If the pattern is not found, the character will be returned unchanged. The optional parameter count is the maximum number of substitutions after pattern matching; count must be a non-negative integer. The default value is 0 which replaces all matches. Example:
#!/usr/bin/python import re phone = "2004-959-559 # This is Phone Number" # Delete Python-style comments num = re.sub(r'#.*$', "", phone) print "Phone Num : ", num # Remove anything other than digits num = re.sub(r'\D', "", phone) print "Phone Num : ", numThe execution result of the above example is as follows:
Phone Num : 2004-959-559 Phone Num : 2004959559
Regular expression modifier - optional flagRegular expression can contain some optional flag modifications character to control matching patterns. The modifier is specified as an optional flag. Multiple flags can be specified by bitwise OR(|) them. For example, re.I | re.M is set to I and M flags: ModifierDescriptionre.IMake matching case-insensitivere.LDo locale-aware matchingre.MMulti-line matching, affects ^ and $re.Smakes . Matches all characters including newlines re.U Parses characters according to the Unicode character set. This flag affects \w, \W, \b, \B.re.X by giving you more flexibility in formatting your regular expressions Written to be easier to understand.
Regular expression pattern
Pattern strings use special syntax to represent a regular expression:
Letters and numbers represent themselves. Letters and numbers in a regular expression pattern match the same string.
Most letters and numbers have different meanings when preceded by a backslash.
Punctuation marks match themselves only if they are escaped, otherwise they represent a special meaning.
The backslash itself needs to be escaped with a backslash.
Since regular expressions usually contain backslashes, you'd better use raw strings to represent them. Pattern elements (such as r'/t', equivalent to '//t') match the corresponding special characters.
The following table lists the special elements in the regular expression pattern syntax. If you use a pattern and provide optional flags arguments, the meaning of some pattern elements will change.
Pattern | Description |
---|---|
^ | Match the beginning of the string |
$ | Matches the end of the string. |
. | Matches any character except newline characters. When the re.DOTALL flag is specified, it can match any character including newline characters. |
[...] | is used to represent a group of characters, listed separately: [amk] matches 'a', 'm' or 'k' |
[^...] | Characters not in []: [^abc] matches characters other than a, b, c. |
re* | Matches 0 or more expressions. |
re+ | Matches one or more expressions. |
#re? | Match 0 or 1 fragment defined by the previous regular expression, non-greedy way |
re{ n} | |
re{ n,} | Exactly matches n previous expressions. |
re{ n, m} | Match n to m times the fragment defined by the previous regular expression, greedy way |
a| b | matches a or b |
(re) | G matches the expression in brackets and also represents a group |
(?imx) | Regular expressions contain three optional flags: i, m, or x. Only affects the area in brackets. |
(?-imx) | Regular expression turns off the i, m, or x optional flags. Only affects the area in brackets. |
(?: re) | Similar to (...), but does not represent a group |
(?imx : re) | Use i, m, or x in parentheses optional flag |
(?-imx: re) | in parentheses Do not use i, m, or x optional flags |
(?#...) | Comments. |
Regular expression exampleCharacter matchingExampleDescriptionpython Matches "python".
Character class
Instance | Description |
---|---|
[Pp]ython | Matches "Python" or "python" |
matches any lowercase letter | |
Match any uppercase letters | |
Match any letters and numbers | |