Four, the regular expression Strings are the most data structure involved in programming, and the need to manipulate strings is almost ubiquitous. For example, to determine whether a string is a legitimate email address, although it can be programmed to extract the substring before and after, and then judge whether it is a word and domain name, but this is not only cumbersome, and the code is difficult to reuse. A regular expression is a powerful weapon used to match strings. Its design idea is to use a descriptive language to define a rule for a string, and any string that conforms to the rule, we think it "matches", otherwise the string is illegal. The following diagram shows the process of using regular expression matching
1. Python-supported regular expression meta-characters and syntax
| Grammar |
Description |
An instance of an expression |
Full-Match string |
| Character |
| General characters |
Match yourself |
Abc |
Abc |
| . |
Match any character "\ n" except In Dotall mode (re. Dotall) can also match newline characters.
|
A.b |
ABC or ABC or A1C, etc. |
[...]
|
The character set [ABC] represents a or B or C, or it can-represent a range such as [a-d] for a or B or C or D |
A[bc]c |
ABC or ADC |
| [^...] |
Non-character sets, which are characters other than [] |
A[^bc]c |
ADC or AEC, etc. |
| Predefined character sets (can also be tied to character sets [...] In |
| \d |
Number: [0-9] |
A\dc |
A1C, etc. |
| \d |
Non-numeric: [^0-9] or [^\d] |
A\dc |
ABC, etc.
|
| \s |
White space character:[< space >\t\n\f\v] |
A\sc |
A B etc |
| \s |
Non-whitespace characters: [^s]
|
A\sc |
ABC, etc. |
| \w |
Alpha-Numeric (word character) [a-za-z0-9] |
A\wc |
ABC or A1C, etc. |
| \w |
Non-alphanumeric (non-word character) [^\w] |
A\wc |
A.C or A_c, etc. |
| Quantity words (used in characters or (...) After grouping) |
| * |
Matches 0 or more expressions. (note including 0 times)
|
abc* |
AB or ABCC |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Boundary match (does not consume characters in the character to be matched) |
| ^ |
Matches the beginning of the string, matching the beginning of each line in multiline mode |
^abc |
ABC or ABCD, etc. |
| $ |
Matches the end of the string, matching the end of each line in multiline mode
|
abc$ |
ABC or 123ABC etc. |
| \a |
Match string start only |
\aabc |
ABC or ABCD, etc.
|
| \z |
Match string End only |
Abc\z |
ABC or 123ABC etc.
|
| \b |
matches a word boundary, which is the position between the word and the 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 '.
|
|
|
| Logic, grouping |
|
|
|
|
|
|
|
|
From for notes (Wiz)
My Python growth path---the fourth day---python Foundation---January 24, 2016 (icy)