本文介绍了ag Silver Searcher:前瞻/后视模式的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Vim 中使用 ag 对字符串/文件搜索效果很好.

I'm using ag in Vim with good results for string/file search.

然而,似乎没有多少文档是如何为 ag 构建模式的.

However, there seems to be not much documentation how patterns are constructed for ag.

我试图在 Practical Vim 书中的一个例子中使用 ag 而不是 vimgrep.

I'm trying to use ag instead of vimgrep in an example from the Practical Vim book.

我想递归地查找目录中所有文件中出现的所有Pragmatic Vim",并将该字符串替换为Practical Vim".某些文件中还有实用书架",该字符串必须保留.

I want to find every occurrence of "Pragmatic Vim" in all files in a directory recursively, and substitute this string with "Practical Vim". There is also "Pragmatic Bookshelf" in some files, and that string must remain.

这本书建议了这种方法:

the book suggests this approach:

/Pragmatic\ze Vim
:vimgrep /<C-r>// **/*.txt

在这一步之后,用 :Qargs 插件命令填充 quickfix 列表,最后 :argdo %s//Practical/g

And after that step, populate quickfix list with :Qargs plugin command, and, finally :argdo %s//Practical/g

现在,我如何使用 ag 指定 /Pragmatic\ze 模式?

Now, how do I specify /Pragmatic\zepattern using ag?

ag 是专为我在这里要做的事情而设计的吗?

Is ag at all designed for what I'm trying to do here?

推荐答案

Silver Searcher 工具使用 PCRE(Perl 兼容正则表达式)语法.因此,您需要使用 Perl 语法来实现 positive lookahead:(?=pattern),而不是 Vim 的 \ze.(\zs 对应的 lookbehind 将是 (?<=pattern).)

The Silver Searcher tool uses PCRE (Perl-Compatible Regular Expression) syntax. So instead of Vim's \ze, you need to use the Perl syntax for positive lookahead: (?=pattern). (The corresponding lookbehind for \zs would be (?<=pattern).)

我在命令行上展示您的示例,但在 Vim 中应该是相同的:

I'm showing your example on the command-line, but it should be identical from within Vim:

$ ag 'Pragmatic(?= Vim)'

这篇关于ag Silver Searcher:前瞻/后视模式的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:21