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]#
[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的,这种行被匹配。

总算在网上找来一个相对简单的例子。 验证如下: [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的,这种行被匹配。
经验分享 程序员 微信小程序 职场和发展