Commonly used matching rules
Match characters
symbol | Matching rules |
---|---|
. | Match any 1 character, except for newline characters\n |
[ ] | This is a set that matches any character in [] |
\d | Match a number, that is, 0-9 |
\D | Match non-digits, i.e. not digits |
\s | Match blank, that is, space, tab key |
\S | Match non-blank |
\w | Match word characters, namely az, AZ, 0-9 |
\W | Match non-word characters |
* | Match the previous character 0 or countless times, it can be dispensable |
+ | Match the previous character appears 1 time or countless times, that is, at least 1 time |
\ ? | Match the previous character 1 or 0 times, that is, either once or not |
{m} | Match the previous character m times |
{m,} | Match the previous character at least m times |
{n,m} | Match the previous character from n to m times |
^ | Match the beginning of the string |
$ | Match end of string |
Group matching
symbol | Matching rules |
---|---|
() | Treat the characters in brackets as a group |
\on one | num can be 1, 2, 3,..., the reference group matches the string |
(?P) | Group aliases |
(?P=name) | Quote the string matched by the name group by alias |
Supplement: |: Match any expression on the left and right
Modifier
Modifier | description |
---|---|
re.I | When matching, ignore the case of letters |
re.L | Do local-aware matching |
re.M | Multi-line matching, affects ^ and $ |
re.S | When matching, all characters including newline |
re.U | Parse characters according to the Unicode character set |
re.X | This flag gives you a more flexible format so that you can write regular expressions easier to understand |
For more details, please refer to: regular expression