Search code examples
regexsublimetext3sublime-syntax

Regex, sublime-syntax file, matching all after a particular character except a specific group


I have a new file extension with a specific syntax, i've created a sublime-syntax file, and i'm trying to highlight certain characters in sublime text editor..

Assuming the following text :

Accept-Language: en-EN
n1.legend=0,1,meta,text,Legend,b1,30 chars max
r1.label=Contain

I want to match all characters after ":" or "=" except the letter "b" followed by one or two numbers (like a placeholder). I tried the following regex :

(?<=[=|:])((?!b[0-9]{1,2}).)*

It works but it doesn't match characters after the "b1" for instance ",30 chars max", why is that ? any help please ? i'm not an expert in regex..

Problem capture :

Sublime tes syntax file

",30 chars max" must be yellow..


Solution

  • To get the matches only (and if supported) you could make use of \G to get repetitive matches and \K to clear the match buffer.

    (?:^[^=:\r\n]+[=:]|\G(?!^))(?:b\d{1,2})?\K.*?(?=b\d{1,2}|$)
    

    Explanation

    • (?: Non capture group
      • ^[^=:\r\n]+[=:] Match either the pattern that starts the string
      • | Or
      • \G(?!^) Assert the positive at the previous match, not at the start
    • ) Close group
    • (?:b\d{1,2})? Optionally match b followed by 2 digits
    • \K Reset the match buffer
    • .*? Match any char except a newline as least as possible (non greedy)
    • (?=b\d{1,2}|$) Positive lookahead, assert what is on the right is either b followed by 2 digits or end of string

    Regex demo