wakey
wakey

Reputation: 2409

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:

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?

Upvotes: 0

Views: 539

Answers (1)

Laurel
Laurel

Reputation: 6173

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.

Upvotes: 1

Related Questions