Commonly used functions and methods of python-re module

Common Regular Expression Properties

  • just the re module function
    compile(patternflags= 0) Compile the regex pattern with any optional tokens, then return a regex object


  • re module functions and methods of regular expression objects 
    match(patternstringflags=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
    None
    search(patternstringflags=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
    None
    findall(patternstring[, flags] ) Finds all (non-repeating) occurrences of a regular expression pattern in a string and returns a list of matches
    finditer(patternstring[, flags] ) Same as findall() , but instead of returning a list, it returns an iterator. For each match, the iterator
    returns a match object
    split(patternstringmax=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(patternreplstringcount=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 itself
    re.S rer.DOTALL .” (点号)通常匹配除了\n(换行符)之外的所有单个字符;该标记表示“.” (点号)
    能够匹配全部字符
    re.X re.VERBOSE 通过反斜线转义, 否则所有空格加上#(以及在该行中所有后续文字)都被忽略,除非
    在一个字符类中或者允许注释并且提高可读性


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325745765&siteId=291194637