Regular expressions Basic syntax meta-character
Code |
Description |
Example |
. |
Match any character except for a line break |
|
[ABC] |
The character set can only represent one character position. Match any one of the characters contained |
|
[^ABC] |
The character set can only represent one character position. Matches any character that drops a character within a collection |
|
[A-z] |
A character range, a collection that represents a character position that matches any one of the characters contained in the |
|
[^a-z] |
A character range, a collection that represents a character position that matches any character that drops a character within a collection |
|
\b |
Match the boundaries of a word |
|
\b |
Do not match the boundaries of a word |
|
\d |
equals [0-9] matches a digit |
|
\d |
Equivalent [^0-9] matches a non-numeric |
|
\s |
Matches 1-bit whitespace characters, including line breaks, tabs, space equivalents [\f\r\n\t\v] |
|
\s |
Match 1-bit non-whitespace characters |
|
\w |
Equivalent [a-za-z0-9_] contains Chinese |
|
\w |
Match characters outside of \w |
|
Escape
- A symbol with a special meaning in a regular expression, which is escaped using \
Repeat
Code |
Description |
Example |
* |
The preceding regular expression repeats 0 or more times |
|
+ |
The preceding regular expression repeats at least once |
|
? |
The preceding regular expression repeats 0 or 1 times |
|
N |
Repeat n times |
|
{N,} |
Repeat more than n times |
|
{N,m} |
Repeat N to M times |
|
Grouping (capturing) assertions
Code |
Description |
Example |
X |
Y |
Match x or Y |
|
(pattern) |
Group numbers are automatically assigned after grouping (capture) starting from 1 You can change the priority \ Number match corresponding grouping (refers to the contents of the group on the previous match) |
|
(?:p Attern) |
Change priority non-grouping only |
|
(? <name>exp) (?name Exp |
The group captures the naming of the Python syntax as a (? P<NAME>EXP) |
|
(? =exp) |
0 width Positive lookahead assertion assertion exp must appear on the right side of the match |
|
(? <=exp) |
0 width is recalling the post assertion assertion that exp must appear on the left side of the match |
|
(?! Exp |
0 width Negative lookahead assertion asserts that exp must not appear on the right side |
|
(? <!exp) |
0 width Negative Review post assertion asserts that Exp must not appear on the left side |
|
(? #comment) |
Comments |
|
Assertion does not occupy a group number
Greed and non-greed
- Default greedy mode, as many matching strings as possible
Code |
Description |
Example |
*? |
Match any number of times and repeat as little as possible |
|
+? |
Match at least once and repeat as little as possible |
|
?? |
Match 0 or 1 times and repeat as little as possible |
|
{n}? |
Match at least n times and repeat as little as possible |
|
{n,m}? |
Match at least N times, up to M times, as few repetitions as possible |
|
Engine options
Code |
Description |
Example |
IgnoreCase |
Ignore case when matching |
Re. Ire.ignorecase |
Singleline |
Single-line mode, can penetrate/n |
Re. Sre.dotall |
Multiline |
Multi-line mode |
Re. Mre.multiline |
Ignorepatternwhitespace |
Ignore expression whitespace character, to use white space characters |
Re. Xre.verbose |
Code Description Example
IgnoreCase match ignores case re. Ire.ignorecase
Singleline single-line mode, which penetrates the/n re. Sre.dotall
Multiline Multi-line mode re. Mre.multiline
Ignorepatternwhitespace ignores the expression whitespace characters to use the white-space character re. Xre.verbose
Use | (bitwise OR) operations in Python to turn on multiple options
Python Regular Expressions RE module compilation
- Re.compile (pattern, flags=0)
- Returns the regular Expression object regex
- The results of the regular expression compilation are saved, and the next time you use the same pattern, you do not need to recompile
One-time match
Regex.match (string[, pos[, Endpos])
- Match from beginning of string to specify start and end position return match object
Regex.search (string[, pos[, Endpos])
- Search from scratch until the first match, specifying the start and end positions to return the match object
- Regex.fullmatch (string[, pos[, Endpos])
- Match the entire string to the regular expression
Full-Text Search
Match substitution
Split string
- Regex.Split (String, maxsplit=0)
Group
Todo:argparse
Python Sixth Week Study notes (2)