2012-04-14 213 views
6

我有以下任务:“ 2”在正则表达式结尾处的含义是什么

歌曲的单词在名为stairway.txt的文件中。

grep -E '(^.{4})(.{2}).*[ ]\2' stairway.txt 

(a) Yes, there are two paths you can go by but in the long run 

(b) Its just a spring clean for the May queen. 

(c) Don't be alarmed now. 

(d) If there's a bustle in your hedgerow. 

(e) Theres still time to change the road you're on. 

我不明白什么是\2在到底是什么意思:以下哪个行会此命令后,打印出来?

+0

你有没有尝试吗?这是功课吗? – Toto 2012-04-14 09:46:58

+0

stairway denied :) – Robbie 2012-04-14 10:17:42

回答

11

这是反向引用。

http://www.selectorweb.com/grep_tutorial.html

向引用是一种表达\ n,其中n是一个数字。它与表达式中第n组圆括号的内容相匹配。

而且,答案是(d):

$ grep -E '(^.{4})(.{2}).*[ ]\2' test.txt 
If there's a bustle in your hedgerow. 
相关问题