简体   繁体   English

使用正则表达式和非贪婪修饰符的vim语法匹配

[英]vim syntax match using regex and non-greedy modifier

I am trying to create a custom syntax that has a structure similar to the following 我正在尝试创建一种自定义语法,其结构类似于以下内容

Title String:
{
   ...,
   ...,
   ...
}

Title String2:
{
   ...,
   {
      ...,
      ...,
      ...
   }
   ...,
   ...,
   ...
}

I have been able to write syn match and syn region statements that detect everything within the { ... } regions, however I have not been able to come up with one that will match the Title Strings. 我已经能够编写用于检测{ ... }区域内所有内容的syn matchsyn region语句,但是我无法提出与Title Strings相匹配的语句。

Here is my region statement: 这是我的区域声明:

syn region dbgMessage start="{" end="}" contains=ALLBUT,dbgMessageHeader

I attempted to add something like this to detect the Title Strings, which I want to be everything/anything up to but not including the opening bracket. 我试图添加类似这样的东西来检测标题字符串,我希望它是包括但不包括开头括号在内的所有内容。

syn match dbgMessageHeader "\v.\{-}\ze(\{)"

My reasoning: 我的推理:

  • .\\{-} should consume every character and be non greedy .\\{-}应该占用所有字符并且不要贪婪
  • \\ze(\\{) should look ahead for an opening bracket and stop when it finds one \\ze(\\{)应该向前寻找一个括号,并在找到一个括号时停止

A bonus challenge is that it would be great if this syntax could correctly detect everything if I get the code in a flattened state, ex: 一个额外的挑战是,如果我使代码处于展平状态,那么该语法可以正确检测所有内容,那就太好了,例如:

Title String: { ..., ..., ... }
Title String2: { ..., { ..., ..., ... } ..., ..., ... }

Again, my current implementation can correctly match everything inside the brackets in both flat and formatted states, so it would be great if I could figure something out that would also match the title strings in both cases. 同样,我当前的实现可以正确地将括号内的所有内容以扁平状态和格式化状态进行匹配,因此,如果我能找出在两种情况下也都可以与标题字符串匹配的内容,那就太好了。

See something that I'm missing? 看到我想念的东西吗?

I'm not very familiar with Vim's regex syntax, but this regex will work for most Perl-based flavors (with the s modifier): 我对Vim的regex语法不是很熟悉,但是此regex可以用于大多数基于Perl的风格(带有s修饰符):

\s([^\s]+):.*?\{

To translate that into Vim, it should be (I think): 要将其转换为Vim,应该是(我认为):

\s\([^\s]\+\):\_.\{-}{

Note that I am using \\_. 请注意,我正在使用\\_. instead of . 代替. to make it act like the s modifier. 使它像s修饰符一样起作用。

Also note that " very magic mode " (whatever that is) will probably mess this up. 另请注意,“ 非常魔术模式 ”(无论是哪种模式 )都可能使此混乱。


It's not easy (if possible) to get the "flat version", since Vim only supports DFA matching. 获得“固定版本”并不容易(如果可能),因为Vim仅支持DFA匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM