正则表达式 - 反向引用 - 单词定界符?

Regex - Backreferencing - word delimiter?

为了通过掌握正则表达式,第 3 版一书理解反向引用,我创建了一个包含以下两行的文件:

the the

the theft

之后我尝试执行这个命令:

:g/\([a-zA-Z]\+\) 

所以它向我显示了两条线,原因很明显

之后我试图通过只捕获第一行来限制结果

:g/\(\<[a-zA-Z]\+\>\) 

即使将单词的分隔符 \< \> 放在括号内,它也向我显示了两行。 下面的命令执行所需的操作(仅显示第一行):

:g/\<\([a-zA-Z]\+\) \>

任何解释为什么括号内的分隔符不起作用?

谢谢

When you are using \(\<[a-zA-Z]\+\>\), it matches the. One thing to know that word boundaries are of zero-width. They can only be used to check some conditions (like for word boundary here), but they cannot be saved in any capturing groups. Capturing groups only saves the matched data and not any assertions.

So, when you are backreferencing it using </code>, it does not remember the word boundaries.</p> </blockquote> <p>而不是你提到的,你必须使用</p> <pre><code>:g/\(\<[a-zA-Z]\+\>\) \<\>

引用 here

中的单词边界

三个不同的位置可以作为单词边界:

Before the first character in the string, if the first character is a word character.

After the last character in the string, if the last character is a word character.

Between two characters in the string, where one is a word character and the other is not a word character.