Regular Expression for Highlighting Indented Bullets in Vim
I'm trying to write some matching rules开发者_高级运维 in a Vim syntax file to highlight indented bullets. My problem is that the syntax highlighting uses a background color, so I would like to match only the bullet character and not the preceding whitespace.
How can I say "match \d., +, -, and * only if preceded by ^\s\{0,1} (but do not match the whitespace)"
With the following matching rules,
syn match notesBullet /^\s*\*/
hi def link notesBullet String
syn match notesNumber /^\s*\d*\./
hi def link notesNumber String
syn match notesMinus /^\s*\-/
hi def link notesMinus Todo
syn match notesPlus /^\s*+/
hi def link notesPlus Plus
I get the following result:
Is there some way to say "if preceded by, but not including" in Vim regex?
In addition to the lookarounds mentioned by Alex, Vim patterns also have the concept of "match start" and "match end" which are denoted by \zs
and \ze
, respectively. Vim will only match when the whole pattern is there, but will exclude everything before \zs
and\or after \ze
(you don't have to specify both) from the match.
So in your case, you can add \zs
after your whitespace pattern and before the bullet/number pattern. For example: /^\s*\zs\d*\./
Is there some way to say "if preceded by" in Vim regex?
Yep, it's called a "zero-width" match, \@<=
(it's known as a "look-behind" in the RE sublanguage of Perl and Python). See here for all details.
精彩评论