正则表达式
基本概念
. |
匹配任意字符 | |
[ ] |
匹配指定字符 | [abcdXYZ0123456789] 等同 [a-dX-Z0-9] |
[^ ] |
排除指定字符 | [^abc] 等同 [^a-c] , 将匹配任意字符但除了 a , b , c |
^ |
行首 | |
$ |
行尾 | |
| |
选择操作 | gray|grey 将会匹配 gray 或 grey |
( ) |
分组(定义一个子表达式) | gr(a|e)y 将会匹配 gray 或 grey |
量化
{n} |
times == n | |
{min,} |
times >= min | |
{min, max} |
times >= min && times <= max | |
? |
等同 {0, 1} |
colou?r 将会匹配 color 和 colour |
* |
等同 {0,} |
ab*c 将会匹配 ac , abc , abbc , abbbc … |
+ |
等同 {1,} |
ab+c 将会匹配 abc , abbc , abbbc … 但不会匹配 ac |
字符集
POSIX | Non-standard | Perl/Tcl | Vim | ASCII (不要忽视’[]’里面的空格;) |
---|---|---|---|---|
[:alnum:] | [A-Za-z0-9] | |||
[:word:] | \w | \w | [A-Za-z0-9_] | |
\W | \W | [^A-Za-z0-9_] | ||
[:alpha:] | \a | [A-Za-z] | ||
[:blank:] | \s | [ \t] | ||
\b | \< \> | (?<=\W)(?=\w)|(?<=\w)(?=\W) | ||
[:cntrl:] | [\x00-\x1F\x7F] | |||
[:digit:] | \d | \d | [0-9] | |
\D | \D | [^0-9] | ||
[:graph:] | [\x21-\x7E] | |||
[:lower:] | \l | [a-z] | ||
[:print:] | \p | [\x20-\x7E] | ||
[:punct:] | [][!“#$%&’()*+,./:;<=>?@\^_`{|}~-] | |||
[:space:] | \s | \_s | [ \t\r\n\v\f] | |
\S | \S | [^ \t\r\n\v\f] | ||
[:upper:] | \u | [A-Z] | ||
[:xdigit:] | \x | [A-Fa-f0-9] |