什么是regexp` ^ \(。* \)\ n \ 1 $`是什么意思?

时间:2013-08-09 05:32:56

标签: regex sed

我碰巧在sed文件中找到了这个正则表达式:

^\(.*\)\n\1$

它解释道:

 This matches a string consisting of two equal substrings separated
 by a newline.

我可以看到它匹配任何字符,以换行符结束,但仅此而已。有人能给我一个解释吗?

2 个答案:

答案 0 :(得分:0)

括号()内的模式称为捕获组

\1表示“与第一个捕获组匹配的任何内容”。

这是一个按角色划分的角色:

 ^     -  matches the beginning of the input
 \(    -  begin capture group (the `(` character must be escaped with a backslash)
 .*    -  zero or more characters
 \)    -  end capture group
 \n    -  newline character
 \1    -  the text "captured" by the first capture group
 $     -  matches the end of the input

答案 1 :(得分:0)

\1部分是指第一个带括号的子表达式,i。即在您的情况下换行前的第一个“任何字符”。

相关问题