import re
Functions in the re module:
1. re.compile (pattern[,flags]): Create a pattern object from a string containing a regular expression.
2. re.findall (pattern, string): List all matches. Return a list.
3. re.sarch (pattern,string[,flags]): Find only the first match.
4. re.match (pattern,string[,flags]): Match only at the beginning of the string , only the first match.
5. re.split(pattern,string[,maxsplit=0]: used to split strings.
6. re.sub(pat, repl, string[,count=0]): Replace all pat matches in the string with repl.
7. re.escape(string): Escape all special regular expression characters in the string.
The methods of re-matching objects:
Through the functions in the re module, when a match is found, a MatchObject object is returned, and some methods can be applied to these objects .
1. group(): A pair of parentheses represent a group in regular expressions , and the number of the group depends on the number of parentheses on the left side of it . Group 0 is the entire pattern .
例:‘There (was a (wee)(cooper)) who (lived in Fyfe)'
Contains the following groups:
0 There was a wee cooper who lived in Fyfe
1 was a wee cooper
2 wee
3 cooper
4 lived in Fyfe
Example 2:
>>>m = re.match(r"(..)+", "a1b2c3")
>>>m.group(1)
'c3'
There is only one parenthesis here, so it is a group.... This group is matched 3 times , and each match of a group overwrites the previous match.
2. start([group]): Returns the start index of the given group match
3. end([group]): Returns the end index of the match for the given group plus +1
4. span([group]): Returns the start and end indices as a tuple .
Greedy and non-greedy modes:
*?, +?, ??, {m,n}? The preceding *, +, ?, etc. are all greedy matching, that is, matching as much as possible, followed by a ? sign to make it lazy matching .
Special characters in regular expressions:
Predefined character sets: