re modular , It provides Perl Style regular expression pattern .re Module enable Python The language has all the regular expression functions .re The module also provides functions that are fully consistent with the functions of these methods , These functions use a pattern string as their first argument .
re.match(pattern, string, flags=0)
Parameters pattern A regular expression that represents a match , Parameters string Represents the string to match , Parameters flags Represents how regular expressions are matched , Multiple flags can be passed by biting OR(|) They specify . The optional values are as follows :
re.match Try to match a pattern... From the beginning of the string , If it's not a successful start match ,match() Just go back to none.
>>> pattern = r"Cats"
>>> string = "Cats are smarter than dogs"
>>> re.match(pattern, string)
<re.Match object; span=(0, 4), match='Cats'>
# Set up flags=re.I Make expressions case insensitive
>>> pattern = r"cats"
>>> re.match(pattern, string, flags=re.I)
<re.Match object; span=(0, 4), match='Cats'>
re.search(pattern, string, flags=0)
Scan the entire string and return the first successful match .
>>> pattern = r"are"
>>> string = "Cats are smarter than dogs"
>>> re.match(pattern, string)
None
>>> re.search(pattern, string)
<re.Match object; span=(5, 8), match='are'>
re.match Match only the beginning of the string , If the string doesn't start with a regular expression , The match fails , The function returns None, and re.search Match the entire string , Until we find a match .
re.sub(pattern, repl, string, count=0, flags=0)
Replace the matching item in the string with the specified string . Parameters pattern A regular expression that represents a match , Parameters repl Represents the replacement string , Parameters string Represents the string to match , Parameters count Represents the maximum number of substitutions after pattern matching , Default 0 Means to replace all matches , Parameters flags Represents how regular expressions match .
# use is Replace in string are
>>> pattern = r"are"
>>> string = "Cats are smarter than dogs"
>>> repl = "is"
>>> re.sub(pattern, repl, string)
'Cats is smarter than dogs'
re.compile(pattern, flags=0)
Compile regular expressions , Generate a regular expression ( Pattern ) object , for match() and search() 、findall()、sub() And other functions .
>>> pattern = re.compile(r"cats", flags=re.I)
>>> pattern
re.compile('cats', re.IGNORECASE)
>>> pattern.match("Cats are smarter than dogs")
<re.Match object; span=(0, 4), match='Cats'>
>>> pattern.search("Cats are smarter than dogs")
<re.Match object; span=(0, 4), match='Cats'>
re.findall(pattern, string, flags=0)
Find all the substrings that the regular expression matches in the string , And return a list , If there are multiple matching patterns , The tuple list is returned , If no match is found , Then return to the empty list .
>>> pattern = r"\d+"
>>> string = "123 is not 12 and 3"
>>> re.match(pattern, string)
<re.Match object; span=(0, 3), match='123'>
>>> re.search(pattern, string)
<re.Match object; span=(0, 3), match='123'>
>>> re.findall(pattern, string)
['123', '12', '3']
Be careful : match and search It's a match , and findall Match all .
re.finditer(pattern, string, flags=0)
Find all the substrings that the regular expression matches in the string , And return them as an iterator .
>>> pattern = r"\d+"
>>> string = "123 is not 12 and 3"
>>> re.findall(pattern, string)
['123', '12', '3']
>>> re.finditer(pattern, string)
<callable_iterator object at 0x0000029B2DEA3EE0>
re.split(pattern, string, maxsplit=0, flags=0)
Split the string according to the matching substring and return to the list . Parameters maxsplit Indicates the number of divisions ,maxsplit=1 Split once , The default is 0, Unlimited times .
>>> pattern = r"\d+"
>>> string = "123 is not 12 and 3"
>>> re.split(pattern, string)
['', ' is not ', ' and ', '']