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

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

the the

the theft

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

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

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

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

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

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

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

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

谢谢

最佳答案

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 \1, it does not remember the word boundaries.

而不是你提到的,你必须使用

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

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

https://stackoverflow.com/questions/37641651/

相关文章:

perl - Docker [for mac] 文件系统变为只读,这几乎破坏了 docker 的所有

twitter-bootstrap - Bootstrap 4 导航栏,带有品牌中心和左侧、中心和右

xcode - 如何禁用 Xcode 4 控制台换行?

https - 从 self 管理的 Let's Encrypt 到 AWS Certificate

spring - 在 Spring RestTemplate 中设置 Authorization h

jenkins - 本地 Jenkins 服务器没有 slave.jar 或 slave-agent

fonts - Webstorm with Java 1.8,字体粗细改变

scala - scala 中的 RESTful http DELETE 方法(玩 2.0)

r - ggplot2 以错误的顺序放置数据标签(geom_text)

c# - 如何在 C# 中创建嵌套(父子)JSON 响应?