vimscript:为什么\S*\%>66c。*/\=替代?=~\\+”返回true - vimscript: why does '\S*\%>66c.*/\=substitute…'=~'\a\+' return true

- 此内容更新于:2015-12-20
主题:

这里是一个映射的内容是“g是当映射产生的消息显然vim考虑的内容相当于因为它适时地打开了,试图找到一个匹配(韦氏词典),该文件的唯一途径是通过提到的测试。有人能向我解释为什么它,还是另一种解释?

原文:

Here is a mapping

map <silent> <2-LeftMouse> lB"gyE  
:if @g=~'ss\d\d\d\.png'  
  :call writefile([@g], "/home/g/xv.vss", "a")  
:elseif @g=~'http'  
  :call writefile([@g], "/home/g/dil.lo", "a")  
:elseif @g=~'_\d\+'  
  :let @/='^'.@g.'$'  
  :norm nzz  
:elseif @g=~'\a\+'  
  :vim /^\cg/ /mnt/1/dict/webster/all.txt  
:else  
  :norm 0y$:@"  
:endif  

Here is the content of "g

\S*\%>66c.*/\=substitute(submatch(0),'

Here are the messages produced when the mapping ran

E303: Unable to open swap file for "=substitute(submatch(0),'/", recovery impossible  
Cannot open file "=substitute(submatch(0),'/"  
"all.txt" 530590 lines --0%--  

Apparently vim considers the content of "g is equivalent to \a\+ because it duly opened all.txt and tried to find a match in it (webster's dictionary) to @g and the only route to that file is thru the mentioned test.

Could anyone explain to me why it does this, or an alternative interpretation?

网友:我问你在做什么,因为它可能很难效仿。但要回答你的问题不会自动为你锚。所以任何一个字母字符匹配。你想要什么。

(原文:Might I ask what you are doing because its rather hard to follow. But to answer your question =~ does not put anchors for you automatically. So anything with an alphabetic character matches. You want something like =~ '^\a\+$'.)

网友:谢谢你,解决了这个问题,但是我怎么给你信用吗?

(原文:Thank you, that solved the problem, but how do I give you credit for it?)

网友:我以后可以发布一个答案。

(原文:I can post an answer later.)

解决方案:
检查的模式匹配。它不添加锚然后如果字符串匹配的任何部分的字符串匹配。如果你想只接受您想要使用而非字母字符。
原文:

=~ checks to see if the pattern matches. It does not add anchors for you so if any part of the string matches then the string counts as matched.

If you want to only accept alphabetic characters you want to use '^\a\+$' instead of \a\+.