常用的正则表达式一览
符号 | 描述 | 例子(表达式) | 例子(匹配的字符串) |
* |
Matches the preceding character, subexpression, or bracketed character, 匹配0次或多次 |
a*b* | aaaaaaaa, aaabbbbb, bbbbbb |
+ |
Matches the preceding character, subexpression, or bracketed character, 匹配1次或多次 |
a+b+ | aaaaaaaab, aaabbbbb, abbbbbb |
[] |
Matches any character within the brackets (i.e., “Pick any one of these 匹配中括号内的任何字符 |
[A-Z]* | APPLE, CAPITALS, QWERTY |
() |
A grouped subexpression (these are evaluated first, in the “order of 子表达式 |
(a*b)* | aaabaab, abaaab, ababaaaaab |
{m, n} |
Matches the preceding character, subexpression, or bracketed character 匹配几遍 |
a{2,3}b{2,3} | aabbb, aaabbb, aabb |
[^] |
Matches any single character that is not in the brackets 取反 |
[^A-Z]* | apple, lowercase, qwerty |
| |
Matches any character, string of characters, or subexpression, separated 或者(匹配其中的任何一个) |
b(a|i|e)d | bad, bid, bed |
. |
Matches any single character (including symbols, numbers, a space, etc.) 匹配任意字符(包括字母,数字,空格,其他符号) |
b.d | bad, b9d, bzd, b$d, b d |
^ |
Indicates that a character or subexpression occurs at the beginning of a 以XX开头 |
^a | apple, a9bp, asdf, a |
\ |
An escape character (this allows you to use “special” characters as their 使用特殊转义符 |
\. \| \\ | . | \ |
$ |
Often used at the end of a regular expression, it means “match this up 以XX结尾 |
[A-Z]*[a-z]*$ | ABCabc, zzzyx, Bob |
?! |
“Does not contain.” This odd pairing of symbols, immediately preceding 不包含 |
^((?![A-Z]).)*$ | no-caps-here, $ymb0ls a4e f!ne |