^[the]:匹配以t或h或e开头的行;
^the:匹配以the开头的行;
[a-z][a-z]*:匹配至少一个小写字母(注意*在RE和wildcard中的区别,可以加上""避免wildcard expansion的处理);
De[Vv]ice\.:匹配DeVice.或者Device.的行;
^ [^|] :不以|开头的行;
[000*]:没有任何的意义;
[^0-9\$]:匹配非数字且非$字符(注意\的作用);
[^0-9A-Za-z] :对于非数字且非字母;
另外,要注意边界符(boundary)的重要性,例如:
- lee@ubuntu:~/shell_study/test_find$ ls -al
- 总用量 16
- drwxr-xr-x 3 lee lee 4096 2011-11-24 20:11 .
- drwxr-xr-x 7 lee lee 4096 2011-11-23 12:23 ..
- drwxr-xr-x 2 lee lee 4096 2011-11-23 10:05 test
- -rwxr--r-- 1 lee lee 0 2011-11-24 20:11 test1
- -rw-r--r-- 1 lee lee 123 2011-11-23 10:08 test.sh
- lee@ubuntu:~/shell_study/test_find$ ls -al | grep "rw"
- drwxr-xr-x 3 lee lee 4096 2011-11-24 20:11 .
- drwxr-xr-x 7 lee lee 4096 2011-11-23 12:23 ..
- drwxr-xr-x 2 lee lee 4096 2011-11-23 10:05 test
- -rwxr--r-- 1 lee lee 0 2011-11-24 20:11 test1
- -rw-r--r-- 1 lee lee 123 2011-11-23 10:08 test.sh
锚点(anchor):用来标示RE匹配的位置:
^:行首,比如^abc 匹配以字符串abc开头的行;
$:行尾,比如abc$匹配以字符串abc结尾的行;
\<:词首,比如\
\>:词尾,比如abc\>匹配以abc结尾的词,并打印该词所在的行。
BRE(Basic Regular Expression)(grep和sed使用)运算符优先级(由高到低):
ERE(egrep和awk使用)运算符优先级:
好像方括号表达式[..]有bug。
匹配包含空格的的空行:'^ *$'
在sed和(grep)中,只有当“^”和"$"分别处于行首和行尾时,才有特殊作用。而在awk中,它们总是特殊的,即使它们可能使编写的正则表达式不匹配任何东西。
在POSIX扩展中,
?等价于\{0,1\}
*等价于\{0,\}
+等价于\{1,\}