grep 與正則表達式分組


總算在網上找來一個相對簡單的例子。

驗證如下:

[root@localhost tmp]# cat test.txt
tsttst tsttsttst
west gao
west abces
[root@localhost tmp]# egrep "w(es)t.*\1" test.txt
west abces
[root@localhost tmp]# grep "w(es)t.*\1" test.txt
grep: Invalid back reference
[root@localhost tmp]# grep -E "w(es)t.*\1" test.txt
west abces
[root@localhost tmp]# grep "w\(es\)t.*\1" test.txt
west abces
[root@localhost tmp]# 

(es)被作為一個組看待,它是一個組,它的名稱是1,然后 .*表示 之后的任意個字符,【\1】指代的是前面用括號括起來的es。

那么  egrep 里的 w(es)t.*\1 就是說 west后面任意字符,再然后再出現es的,這種行被匹配。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM