ag Silver Searcher: what are the rules for patterns?

假装没事ソ 提交于 2019-12-04 15:37:29

问题


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

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

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

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

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

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

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


回答1:


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).)

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

$ ag 'Pragmatic(?= Vim)'


来源:https://stackoverflow.com/questions/22190334/ag-silver-searcher-what-are-the-rules-for-patterns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!