“\ 2”在正则表达式的末尾是什么意思

时间:2012-04-14 09:27:24

标签: regex linux bash

我有以下任务:

歌曲的单词位于名为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是什么意思?

1 个答案:

答案 0 :(得分:13)

这是一个反向引用。

来自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.