Vim-如何根据搜索进行搜索和重新放置 [英] Vim - How to search and reaplace based on search
问题描述
我想做什么:
每次我找到基于s[w|l].*[0-9]\.\*
的内容时,都用%s/\.\*/\\\\\.\.\*/g
Every time i find something based on s[w|l].*[0-9]\.\*
replace the end of that string\search .* with %s/\.\*/\\\\\.\.\*/g
已经尝试使用标准搜索和替换,但无法做到.
Already tried with standard search and replace, but couldn't do it.
谢谢
推荐答案
只需将搜索模式包装在转义括号\(\)
中,并在替换字符串中使用后向引用\1
:
Just wrap your search pattern in escaped parentheses \(\)
, and use a backreference \1
in the replacement string:
:%s/\(s[w|l].*[0-9]\)\.\*/\1\\\\.*/g
您的搜索字符串中可以有几个\(\)
子表达式,并且可以通过\1
,\2
,...在搜索字符串本身和替换字符串中使用它们.子表达式只是按搜索字符串中出现的顺序编号.
You can have several \(\)
subexpressions in your search string, and can use them both in the search string itself and in the replacement string via \1
, \2
, ... . Subexpressions are simply numbered by order of occurence in the search string.
这篇关于Vim-如何根据搜索进行搜索和重新放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!