Common Regular Expression Properties
- just the re module function
compile(pattern,flags= 0) Compile the regex pattern with any optional tokens, then return a regex object - re module functions and methods of regular expression objects
match(pattern,string,flags=0) Attempts to match strings using a regular expression pattern with optional tokens. If the match is successful, it returns the
match object; if it fails, it returns Nonesearch(pattern,string,flags=0) Search for the first occurrence of a regular expression pattern in a string using optional tokens. If the match is successful, it returns the match
object; if it fails, it returns Nonefindall(pattern,string[, flags] )① Finds all (non-repeating) occurrences of a regular expression pattern in a string and returns a list of matches finditer(pattern,string[, flags] )② Same as findall() , but instead of returning a list, it returns an iterator. For each match, the iterator
returns a match objectsplit(pattern,string,max=0)③ According to the pattern separator of the regular expression, the split function splits the string into a list, and then returns a
list of successful matches, separated by up to max times (by default, all matching positions are split)sub(pattern,repl,string,count=0)③ Use repl to replace all occurrences of the regular expression pattern in the string, unless count is defined , otherwise
all occurrences will be replaced (see also the subn() function, which returns the number of replacement operations)purge() Clear implicitly compiled regex patterns - Commonly used match object methods (check documentation for more info)
group(num=0) Returns the entire match object, or a specific subgroup numbered num groups(default=None) Returns a tuple containing all matching subgroups (or an empty tuple if no match was successful) groupdict(default=None) Returns a dictionary containing all matching named subgroups with all subgroup names as dictionary keys (or
an empty dictionary if no match was successful) - Common module attributes (tags used for most regular expression functions)
re.I、 re.IGNORECASE case-insensitive match re.L、 re.LOCALE Matching via \w , \W , \b , \B , \s , \S depending on the locale used re.M、 re.MULTILINE ^ and $ match the beginning and end of a line in the target string, respectively, rather than strictly matching the beginning
and end of the entire string itselfre.S、 rer.DOTALL “.” (点号)通常匹配除了\n(换行符)之外的所有单个字符;该标记表示“.” (点号)
能够匹配全部字符re.X、 re.VERBOSE 通过反斜线转义, 否则所有空格加上#(以及在该行中所有后续文字)都被忽略,除非
在一个字符类中或者允许注释并且提高可读性