正则表达式末尾的"\2"是什么意思?

7
我有以下任务:
一首歌曲的歌词保存在名为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是什么意思?

你试过了吗?这是作业吗? - Toto
1个回答

15

这是一个反向引用。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接