Vim命令中的\@<=和\@=代表什么意思?

3

无法理解Benoit在此帖子中的回答中\@<=\@=的含义,有人可以帮忙解释一下吗?


3
请参阅:h /\@=':h /\@<=。帮助页面提供了非常清晰的解释。 - FDinoff
@FDinoff 找不到帮助文件:E149: 对于 /\@=',很抱歉没有帮助,我正在使用 Mac OS X 终端上的 Vim v7.3。 - George
@FDinoff 哦,我通过删除您的尾随引号得到了它们 :D - George
2
是的,我格式化得很差。它应该是 :h /\@=:h /\@<= - FDinoff
@FDinoff 我还是不明白“与前面的原子匹配,但不消耗字符”的意思,请给更多的例子。 - George
1个回答

3

来自Vim模式匹配文档

\@= Matches the preceding atom with zero width. {not in Vi}
    Like "(?=pattern)" in Perl.
    Example         matches 
    foo\(bar\)\@=       "foo" in "foobar"
    foo\(bar\)\@=foo    nothing

                            */zero-width*
    When using "\@=" (or "^", "$", "\<", "\>") no characters are included
    in the match.  These items are only used to check if a match can be
    made.  This can be tricky, because a match with following items will
    be done in the same position.  The last example above will not match
    "foobarfoo", because it tries match "foo" in the same position where
    "bar" matched.

    Note that using "\&" works the same as using "\@=": "foo\&.." is the
    same as "\(foo\)\@=..".  But using "\&" is easier, you don't need the
    braces.


\@<=    Matches with zero width if the preceding atom matches just before what
    follows. |/zero-width| {not in Vi}
    Like '(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
    Example         matches 
    \(an\_s\+\)\@<=file "file" after "an" and white space or an
                end-of-line
    For speed it's often much better to avoid this multi.  Try using "\zs"
    instead |/\zs|.  To match the same as the above example:
        an\_s\+\zsfile

    "\@<=" and "\@<!" check for matches just before what follows.
    Theoretically these matches could start anywhere before this position.
    But to limit the time needed, only the line where what follows matches
    is searched, and one line before that (if there is one).  This should
    be sufficient to match most things and not be too slow.
    The part of the pattern after "\@<=" and "\@<!" are checked for a
    match first, thus things like "\1" don't work to reference \(\) inside
    the preceding atom.  It does work the other way around:
    Example         matches 
    \1\@<=,\([a-z]\+\)  ",abc" in "abc,abc"

1
还是不明白"Matches the preceding atom with zero width." 是什么意思,请给更多的示例好吗? - George
1
请查看此SO问题的第一个答案:零宽度断言在该答案中得到了很好的解释:https://dev59.com/6W3Xa4cB1Zd3GeqPdlhj - vaichidrewar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接