Re.match function
Re.match attempts to match a pattern from the starting position of the string, and if the match is not successful, match () returns none.
string, flags=0) pattern matches The string of the regular expression string to match. Flags flags that govern how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
We can use the group (NUM) or groups () matching object function to get a matching expression. Groups () prints the entire tuple group () prints the entire string, group (1) prints the first element group (1,3) print 第1-3个 element span() matches the position [start, end] starting at 0
Re.search method
Re.search scans the entire string and returns the first successful match.
string, flags=0)
Pattern matches the string of the regular expression string to match. Flags flags that govern how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
The match succeeds Re.search method returns a matching object, otherwise none is returned.
We can use the group (NUM) or groups () matching object function to get a matching expression. Groups () prints the entire tuple group () prints the entire string, group (1) prints the first element group (1,3) print 第1-3个 element span() matches the position [start, end]
Retrieving and replacing
The Python re module provides re.sub to replace matches in a string.
string, count=0) Pattern: The pattern string in the regular. REPL: The replacement string, or a function. string0 indicates that all matches are replaced.
Compile function
The compile function compiles a regular expression and generates a regular expression (Pattern) object for use by the match () and search () functions.
re.compile (pattern[, Flags]) pattern: a regular expression in the form of a string flags is optional, indicating a matching pattern, such as ignoring case, multiline mode, etc., with the following parameters: Re. I Ignore case Re. L represents the special character set \w, \w, \b, \b, \s, \s dependent on the current environment re. M Multi-line mode re. S is ' and any character including newline characters (' do not include line breaks ') re. U represents a special character set \w, \w, \b, \b, \d, \d, \s, \s dependent on the Unicode character attribute database re. X to increase readability, ignore spaces and comments that follow
FindAll
Finds all substrings that match the regular expression in the string, returns a list, and returns an empty list if no match is found.
FindAll (string[, pos[, Endpos]])string0. Endpos an optional parameter that specifies the end position of the string, which defaults to the length of the string.
Re.finditer
Similar to FindAll, finds all substrings that match the regular expression in the string and returns them as an iterator.
string, flags=0) pattern matches The string of the regular expression string to match. Flags -optional flags
Re.split
The Split method returns the list after splitting the string by a substring that can be matched, using the following form:
string [, maxsplit=0, flags=0]) pattern matches The string of the regular expression string to match. Maxsplit Number of separators, maxsplit=10, unlimited number of times. Flags -optional flags
| Mode |
Description |
| ^ |
Matches the beginning of a string |
| $ |
Matches the end of the string. |
| . |
Matches any character, except the newline character, when re. When the Dotall tag is specified, it can match any character that includes a line feed. |
| [...] |
Used to represent a set of characters, listed separately: [AMK] matches ' a ', ' m ' or ' K ' |
| [^...] |
Characters not in []: [^ABC] matches characters other than a,b,c. |
| Tel |
Matches 0 or more expressions. |
| Tem |
Matches 1 or more expressions. |
| Re? |
Matches 0 or 1 fragments defined by a preceding regular expression, not greedy |
| re{N} |
|
| re{N,} |
Exact match n preceding expression. |
| re{N, m} |
Matches N to M times the fragment defined by the preceding regular expression, greedy way |
| a| B |
Match A or B |
| (RE) |
The G matches the expression in parentheses, and also represents a group |
| (? imx) |
The regular expression consists of three optional flags: I, M, or X. Affects only the areas in parentheses. |
| (?-imx) |
The regular expression closes I, M, or x optional flag. Affects only the areas in parentheses. |
| (?: RE) |
A similar (...), but does not represent a group |
| (? imx:re) |
Use I, M, or x optional flag in parentheses |
| (?-imx:re) |
I, M, or x optional flags are not used in parentheses |
| (?#...) |
Comments. |
| (? = re) |
Forward positive qualifiers. If a regular expression is included, ... Indicates that a successful match at the current position succeeds or fails. But once the contained expression has been tried, the matching engine is not improved at all, and the remainder of the pattern attempts to the right of the delimiter. |
| (?! Re) |
Forward negative qualifier. As opposed to a positive qualifier, when the containing expression cannot match the current position of the string |
| (?> re) |
Match the standalone mode, eliminating backtracking. |
| \w |
Match Alpha-Numeric |
| \w |
Match non-alphanumeric numbers |
| \s |
Matches any whitespace character, equivalent to [\t\n\r\f]. |
| \s |
Match any non-null character |
| \d |
Match any number, equivalent to [0-9]. |
| \d |
Match any non-numeric |
| \a |
Match string start |
| \z |
Matches the end of the string, if there is a newline, matches only the ending string before the line break. C |
| \z |
Match string End |
| \g |
Matches the position where the last match was completed. |
| \b |
Matches a word boundary, which is the position between a word and a space. For example, ' er\b ' can match ' er ' in ' never ', but not ' er ' in ' verb '. |
| \b |
Matches a non-word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '. |
| \ n, \ t, et. |
Matches a line break. Matches a tab character. such as |
| \1...\9 |
Matches the contents of the nth grouping. |
| \10 |
Matches the contents of the nth grouping, if it is matched. Otherwise, it refers to an expression of octal character code. |
Python regular-expression learning