Regular expression
A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).
常见正则表达式符号
| 限定符 (Quantifier) | 元字符(meta-characters) |
| a* | a 出现0次或多次 | d | 匹配数字字符 |
| a+ | a 出现1次或多次 | D | 匹配非数字字符 |
| a? | a 出现0次或1次 | w | 匹配单词字符(英文、数字、下划线) |
| a{6} | a 出现6次 | W | 匹配非单词字符 |
| a{2,6} | a出现2-6次 | s | 匹配空白符(包含换行符、Tab) |
| a{2,} | a出现两次以上 | S | 匹配非空白字符 |
| 或运算符 (Or Operator) | . | 匹配任意字符(换行符除外) |
| (a|b) | 匹配a或者b | b | 标注字符的边界(全字匹配) |
| (ab)|(cd) | 匹配ab或者cd | ^ | 匹配行首 |
| 字符类 (Character Classes) | $ | 匹配行尾 |
| [abc] | 匹配a或者b或者c | 贪婪/懒惰匹配(Greedy/Lazy Match) |
| [a-c] | 同上 | <.+> | 默认贪婪匹配“任意字符” |
| [a-zA-Z0-9] | 匹配大小写字母以及数字 | <.+?> | 懒惰匹配“任意字符” |
| [^0-9] | 匹配非数字字符 | | |
拓展表示法
| Notation | Description | Example Regex |
|---|
| (?iLmsux) | Embed one or more special “flags”parameters within the regex itself (vs. via function/method) | (?x),(?im) |
| (?:…) | Signifies a group whose match is not saved | (?:w+.)* |
| (?P…) | Like a regular group match only identified with name rather than a numeric ID | (?P) |
| (?P=name) | Matches text previously grouped by (?P)in the same string | (?P=data) |
| (?#…) | Specifies a comment, all contents within ignored | (?#comment) |
| (?=…) | Matches if…comes next without consuming input string; called positive lookahead assertion | (?=.com) |
| (?!..) | Matches if … doesn’t come next without consuming input; called negative lookahead assertion | (?!.net) |
| (?<=…) | Matches if … comes prior without consuming input string; called positive look behind assertion | (?<=800-) |
| (? | Matches if … doesn’t come prior without consuming input; called negative look behind assertion | (? |
| (?(id/name)Y|N) | Conditional match of regex Y if group with given id or name exists else N; N is optional | (?(1)y|x) |