regular expressions (RE) 简介

  re模块是python中处理正在表达式的一个模块

  1 r"""Support for regular expressions (RE).
  2 
  3 This module provides regular expression matching operations similar to
  4 those found in Perl.  It supports both 8-bit and Unicode strings; both
  5 the pattern and the strings being processed can contain null bytes and
  6 characters outside the US ASCII range.
  7 
  8 Regular expressions can contain both special and ordinary characters.
  9 Most ordinary characters, like "A", "a", or "0", are the simplest
 10 regular expressions; they simply match themselves.  You can
 11 concatenate ordinary characters, so last matches the string 'last'.
 12 
 13 The special characters are:
 14     "."      Matches any character except a newline.
 15     "^"      Matches the start of the string.
 16     "$"      Matches the end of the string or just before the newline at
 17              the end of the string.
 18     "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
 19              Greedy means that it will match as many repetitions as possible.
 20     "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
 21     "?"      Matches 0 or 1 (greedy) of the preceding RE.
 22     *?,+?,?? Non-greedy versions of the previous three special characters.
 23     {m,n}    Matches from m to n repetitions of the preceding RE.
 24     {m,n}?   Non-greedy version of the above.
 25     "\\"     Either escapes special characters or signals a special sequence.
 26     []       Indicates a set of characters.
 27              A "^" as the first character indicates a complementing set.
 28     "|"      A|B, creates an RE that will match either A or B.
 29     (...)    Matches the RE inside the parentheses.
 30              The contents can be retrieved or matched later in the string.
 31     (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
 32     (?:...)  Non-grouping version of regular parentheses.
 33     (?P<name>...) The substring matched by the group is accessible by name.
 34     (?P=name)     Matches the text matched earlier by the group named name.
 35     (?#...)  A comment; ignored.
 36     (?=...)  Matches if ... matches next, but doesn't consume the string.
 37     (?!...)  Matches if ... doesn't match next.
 38     (?<=...) Matches if preceded by ... (must be fixed length).
 39     (?<!...) Matches if not preceded by ... (must be fixed length).
 40     (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
 41                        the (optional) no pattern otherwise.
 42 
 43 The special sequences consist of "\\" and a character from the list
 44 below.  If the ordinary character is not on the list, then the
 45 resulting RE will match the second character.
 46     \number  Matches the contents of the group of the same number.
 47     \A       Matches only at the start of the string.
 48     \Z       Matches only at the end of the string.
 49     \b       Matches the empty string, but only at the start or end of a word.
 50     \B       Matches the empty string, but not at the start or end of a word.
 51     \d       Matches any decimal digit; equivalent to the set [0-9] in
 52              bytes patterns or string patterns with the ASCII flag.
 53              In string patterns without the ASCII flag, it will match the whole
 54              range of Unicode digits.
 55     \D       Matches any non-digit character; equivalent to [^\d].
 56     \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
 57              bytes patterns or string patterns with the ASCII flag.
 58              In string patterns without the ASCII flag, it will match the whole
 59              range of Unicode whitespace characters.
 60     \S       Matches any non-whitespace character; equivalent to [^\s].
 61     \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
 62              in bytes patterns or string patterns with the ASCII flag.
 63              In string patterns without the ASCII flag, it will match the
 64              range of Unicode alphanumeric characters (letters plus digits
 65              plus underscore).
 66              With LOCALE, it will match the set [0-9_] plus characters defined
 67              as letters for the current locale.
 68     \W       Matches the complement of \w.
 69     \\       Matches a literal backslash.
 70 
 71 This module exports the following functions:
 72     match     Match a regular expression pattern to the beginning of a string.
 73     fullmatch Match a regular expression pattern to all of a string.
 74     search    Search a string for the presence of a pattern.
 75     sub       Substitute occurrences of a pattern found in a string.
 76     subn      Same as sub, but also return the number of substitutions made.
 77     split     Split a string by the occurrences of a pattern.
 78     findall   Find all occurrences of a pattern in a string.
 79     finditer  Return an iterator yielding a match object for each match.
 80     compile   Compile a pattern into a RegexObject.
 81     purge     Clear the regular expression cache.
 82     escape    Backslash all non-alphanumerics in a string.
 83 
 84 Some of the functions in this module takes flags as optional parameters:
 85     A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \D
 86                    match the corresponding ASCII character categories
 87                    (rather than the whole Unicode categories, which is the
 88                    default).
 89                    For bytes patterns, this flag is the only available
 90                    behaviour and needn't be specified.
 91     I  IGNORECASE  Perform case-insensitive matching.
 92     L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.
 93     M  MULTILINE   "^" matches the beginning of lines (after a newline)
 94                    as well as the string.
 95                    "$" matches the end of lines (before a newline) as well
 96                    as the end of the string.
 97     S  DOTALL      "." matches any character at all, including the newline.
 98     X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.
 99     U  UNICODE     For compatibility only. Ignored for string patterns (it
100                    is the default), and forbidden for bytes patterns.
101 
102 This module also defines an exception 'error'.
103 
104 """
View Code

相关文章: