2012-04-14 317 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. 
相關問題