gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:20:56 25 4
gpt4 key购买 nike

为了通过掌握正则表达式,第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 aword character.

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

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

关于正则表达式 - 反向引用 - 单词定界符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com