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

标签 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是什么意思?

最佳答案

这是一个反向引用。

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

Backreference is an expression \n where n is a number. It matches the contents of the n'th set of parentheses in the expression.

另外,答案是(d):

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

关于regex - 正则表达式末尾的 "\2"是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10152512/

相关文章:

linux - 使用 makefile 脚本将返回值的值增加 1

bash - CD $PATH : No such file or directory

regex - Ansible 和 regex_search

javascript正则表达式检查字符和数字以外的内容

Linux-ls If 语句

linux - awk 语句生成额外的输出行;跳过空白输入行

linux - 如何在 bash shell 脚本中计算文件中每一行的中位数

regex - linux通过sed截取正则表达式模式

php - 匹配、评估和替换 BBCode 样式标签

c - Linux 终端文件打开