1. 擴展正則表達式
擴展正則表達式 ERE Extended Regular Expressions 比基本正則表達式BRE 擁有更強大的功能。
2. ERE字符
egrep表示使用擴展正則表達式,
可以用 grep -E 代替
+ 重復前面字符一次或多次
要匹配 god , good, goood 使用:
grep -E 'go+d' test
? 重復前面字符0次或一次
匹配gd 或 god, 使用
grep -E 'go?d' test
| 表示 或
匹配good或bad,使用
grep -E 'good|bad' test
() 分組
要查找glad 或good, g和d相同,把la 和oo分組:
grep -E 'g(la|oo)d' test
()+ 多個重復組的判斷
匹配 AabcabcabcC 中間abc為多組
grep -E 'A(abc)+C' test
3. 簡寫
簡寫的特殊字符
Character Class Abbreviations
\d Match any character in the range 0 - 9 (equivalent of POSIX [:digit:])
\D Match any character NOT in the range 0 - 9 (equivalent of POSIX [^[:digit:]])
\s Match any whitespace characters (space, tab etc.). (equivalent of POSIX [:space:] EXCEPT VT is not recognized)
\S Match any character NOT whitespace (space, tab). (equivalent of POSIX [^[:space:]])
\w Match any character in the range 0 - 9, A - Z and a - z (equivalent of POSIX [:alnum:])
\W Match any character NOT the range 0 - 9, A - Z and a - z (equivalent of POSIX [^[:alnum:]])
Positional Abbreviations
\b Word boundary. Match any character(s) at the beginning (\bxx) and/or end (xx\b) of a word, thus \bton\b will find ton but not tons, but \bton will find tons.
\B Not word boundary. Match any character(s) NOT at the beginning(\Bxx) and/or end (xx\B) of a word, thus \Bton\B will find wantons but not tons, but ton\B will find both wantons and tons.
Linux正則表達式特性及BRE與ERE的區別 http://www.linuxidc.com/Linux/2014-03/99152.htm
grep使用簡明及正則表達式 http://www.linuxidc.com/Linux/2013-08/88534.htm
正則表達式的用法 http://www.linuxidc.com/Linux/2013-03/81897.htm
正則表達式之零寬斷言 http://www.linuxidc.com/Linux/2013-03/81897.htm
Linux中正則表達式與文件格式化處理命令(awk/grep/sed) http://www.linuxidc.com/Linux/2013-03/81018.htm
基礎正則表達式 http://www.linuxidc.com/Linux/2014-09/106296.htm