在 Vim 中跳过搜索并替换标题后的内容

在 Vim 中跳过搜索并替换标题后的内容

我必须替换一个模式,但我不想在特定单词之后替换它。

这是我的程序:

: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是该行中的唯一单词。根据需要修改正则表达式。)

相关内容