各种语言工具正则表达式语法比较
grep 2.5.1 | egrep 2.5.1 | sed 3.02 sed 4.07 |
awk 3.1.1 | perl 5.8.0 | vim 6.1 | JavaScript | |
转义 | \ | \ | \ | \ | \ | \ | \ |
行头 | ^ | ^ | ^ | ^ | ^ | ^ | ^ |
行尾 | $ | $ | $ | $ | $ | $ | $ |
n个 {n} {m,n} {m,} {,n} | \{n\} | {n} | \{n\} | {n}或\{n\} 仅定义 --posix 或 --re-interval有效(要表达}和{,得用\\{和\\} 没有定义--posix或--re-interval时,不能用{n}的语法, \}\{和}{同义 | {n} | \{n\} | {n} |
{0,}
|
* | * | * | *或\*, (要表达*,得用\\*) | * | * | * |
{1,}
|
\+ | + | \+ | +或\+, (要表达+, 得用\\+) | + | \+ | + |
{0,1}
|
\? | ? | \? | ?或\?, (要表达?, 得用\\?) | ? | \? | ? |
任意字符 | . | . | . | . 含\n. | . /s修饰后则含\n | . 除\n | . 除\n |
(pat) 匹配并获结果 | patpat | (pat) | patpat | (pat)或patpat (要表达括号,用\\( \\) ) | (pat) | patpat | (pat) |
(?:pat) 匹配但不获结果 | (?:pat) | 不支持 | (?:pat) | ||||
(?=pat) 等于预查 | (?=pat) | 不支持 | (?=pat) | ||||
(?!pat) 不等预查 | (?!pat) | 不支持 | (?!pat) | ||||
| 或 | \| | | | \| | |或\| (要表达|,得用\\|) | | | \| | | |
其中任意字符 | [xyz] | [xyz] | [xyz] | [xyz] | [xyz] | [xyz] | [xyz] |
[.ch.] [=ch=] | [.ch.] | ||||||
单词边界 \b | \b | \b | \b | \b | \b | ||
非单词边界 \B | \B | \B | \B | \B | \B | ||
单词左右边界 <> | \< \> | \< \> | \< \> | 不支持 (><和\>\<和\\>\\<同义 | 不支持(><和\>\<同义 | \< \> | 不支持(><和\>\<同义 |
控制字符 /cx | \cx | \cx | \cx | ||||
数字\d | \d | \d | \d | ||||
非数字\D | \D | \D | \D | ||||
换页 \f | 高版本支持 | \f | \f | 另义 \f表示文件名字符 | \f | ||
换行 \n | \n | \n | \n | \n | |||
回车 \r | \r | \r | \r | \r | \r | ||
空白 \s | \s | \s | \s | ||||
非空白 \S | \S | \S | \S | ||||
制表符 \t | 高版本支持 | \t | \t | \t | \t | ||
垂直制表符 \v | 高版本支持 | \v | \v | 另义 \v表示very magic | \v | ||
单词字符 \w [A-Za-z0-9_] | \w | \w | \w | \w | \w | \w | |
非单词字符 \W [^A-Za-z0-9] | \W | \W | \W | \W | \W | \W | |
\xn 16进制 | 高版本支持 | \xn | \xn | 另义 \x表示[0-9A-Za-z] | \xn | ||
\n 八进制 | \n | \n | \n | ||||
\n 后向引用 | \n | \n | \n | \n 仅取结果可用 | \n | \n 仅取结果可用 | \n |
[:alnum:] 字母和数字 | [:alnum:] | [:alnum:] | [:alnum:] | [:alnum:] | [:alnum:] | [:alnum:] | |
[:alpha:] 字母 | [:alpha:] | [:alpha:] | [:alpha:] | [:alpha:] | [:alpha:] | [:alpha:] | |
[:cntrl:] 控制字符 | [:cntrl:] | [:cntrl:] | [:cntrl:] | [:cntrl:] | [:cntrl:] | [:cntrl:] | |
[:digit:] 数字 | [:digit:] | [:digit:] | [:digit:] | [:digit:] | [:digit:] | [:digit:] | |
[:graph:] 可打印字符(不含空格) | [:graph:] | [:graph:] | [:graph:] | [:graph:] | [:graph:] | [:graph:] | |
[:lower:] 小写 | [:lower:] | [:lower:] | [:lower:] | [:lower:] | [:lower:] | [:lower:] | |
[:print:] 可打印字符(含空格) | [:print:] | [:print:] | [:print:] | [:print:] | [:print:] | [:print:] | |
[:punct:] 标点 | [:punct:] | [:punct:] | [:punct:] | [:punct:] | [:punct:] | [:punct:] | |
[:space:] 空格 | [:space:] | [:space:] | [:space:] | [:space:] | [:space:] | [:space:] | |
[:upper:] 大写字母 | [:upper:] | [:upper:] | [:upper:] | [:upper:] | [:upper:] | [:upper:] | |
[:xdigit:] 16进制数字 | [:xdigit:] | [:xdigit:] | [:xdigit:] | [:xdigit:] | [:xdigit:] | [:xdigit:] | |
[:return:] | [:return:] | ||||||
[:tab:] | [:tab:] | ||||||
[:escape:] | [:escape:] | ||||||
[:backspace:] | [:backspace:] |