在VimScript中=〜意味着什么?

puk*_*puk 23 vim operators

我无法在谷歌或此处或帮助文件中找到答案.

if "test.c" =~ "\.c"
Run Code Online (Sandbox Code Playgroud)

起初我认为=~意思是结束,但观察这些结果:

Command                               Result
echo "test.c" =~ "\.c"                1
echo "test.c" =~ "\.pc"               0
echo "test.pc" =~ "\.c"               1
echo "testc" =~ "\.c"                 1
echo "ctest" =~ "\.c"                 1
echo "ctestp" =~ "\.pc"               0
echo "pctestp" =~ "\.pc"              0
echo ".pctestp" =~ "\.pc"             0
Run Code Online (Sandbox Code Playgroud)

解释会很棒.尝试解密VimScript的网站链接会更好.

Mic*_*ski 49

从Vim文档中,它在左侧内部执行右操作数(作为模式)的模式匹配.

对于字符串,还有两个项目:

    a =~ b      matches with
    a !~ b      does not match with
Run Code Online (Sandbox Code Playgroud)

左侧项"a"用作字符串.正确的项目"b"用作模式,就像用于搜索的模式一样.例:

    :if str =~ " "
    :  echo "str contains a space"
    :endif
    :if str !~ '\.$'
    :  echo "str does not end in a full stop"
    :endif
Run Code Online (Sandbox Code Playgroud)

您可以再次尝试测试用例.例如,我与你的不一致:

echo ".pctestp" =~ "\.pc"             1
Run Code Online (Sandbox Code Playgroud)

双引号与单引号似乎会影响反斜杠的解释方式:

echo "test.pc" =~ "\.c"               1
echo "test.pc" =~ '\.c'               0
Run Code Online (Sandbox Code Playgroud)


icy*_*com 5

从文档: