![在 Vim 中跳过搜索并替换标题后的内容](https://linux22.com/image/41406/%E5%9C%A8%20Vim%20%E4%B8%AD%E8%B7%B3%E8%BF%87%E6%90%9C%E7%B4%A2%E5%B9%B6%E6%9B%BF%E6%8D%A2%E6%A0%87%E9%A2%98%E5%90%8E%E7%9A%84%E5%86%85%E5%AE%B9.png)
我必须替换一个模式,但我不想在特定单词之后替换它。
这是我的程序:
:vnoremap ::silent! call Bib()
function! Bib()
%s/\s*\n*{\\&}\s*\n*/ /g
%s/\([A-Z]\)\.\([A-Z]\)\./\1\. \2\./g
"%s/\(\w*\-\w*\|\w*\),\s*\n*\([A-Z]\)\./\r\\snm{\1}\r\2\./g
endfunc
我不希望在特定单词之后使用此搜索模式:“参考文献”
答案1
要匹配“某物”,但不在特定“单词”之后,您可以使用\@<!
;
/\(word\)\@<! something/
用“somethingelse”替换“something”,但前提是“something”不在“word”之后:
:%s/\(word\)\@<! something/ somethingelse/
从里面vim
,显示描述:help /\@<!
:
\@<! Matches with zero width if the preceding atom does NOT match just
before what follows. Thus this matches if there is no position in the
current or previous line where the atom matches such that it ends just
before what follows. |/zero-width| {not in Vi}
Like "(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
The match with the preceding atom is made to end just before the match
with what follows, thus an atom that ends in ".*" will work.
Warning: This can be slow (because many positions need to be checked
for a match). Use a limit if you can, see below.
答案2
您可以%
尝试使用范围地址:
0,/^References$/s/\s*\n*{\\&}\s*\n*/ /g
0,/^References$/s/\([A-Z]\)\.\([A-Z]\)\./\1\. \2\./g
0,/^References$/s/\(\w*\-\w*\|\w*\),\s*\n*\([A-Z]\)\./\r\\snm{\1}\r\2\./g
(假设这References
是该行中的唯一单词。根据需要修改正则表达式。)