bash 正则表达式末尾的“\2”是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10152512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "\2" mean at the end of a regular expression
提问by Zippie
I have the following assignment:
我有以下任务:
Words of a song are in a file called stairway.txt. Which of the following lines will be printed out after this command:
歌曲的词在名为 stairway.txt 的文件中。执行此命令后将打印出以下哪几行:
grep -E '(^.{4})(.{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.
I don't understand what does the \2at the end mean?
我不明白\2最后的意思是什么?
回答by Delan Azabani
It is a backreference.
这是一个反向引用。
From http://www.selectorweb.com/grep_tutorial.html:
从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.
反向引用是一个表达式 \n,其中 n 是一个数字。它匹配表达式中第 n 组括号的内容。
Also, the answer is (d):
另外,答案是(d):
$ grep -E '(^.{4})(.{2}).*[ ]' test.txt
If there's a bustle in your hedgerow.

