简体   繁体   中英

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.

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

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):

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

To translate that into Vim, it should be (I think):

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

Note that I am using \\_. instead of . to make it act like the s modifier.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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