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

290 Views Asked by At

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..

2

There are 2 best solutions below

2
The fourth bird On BEST ANSWER

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

1
simon-pearson On

If from the line 0,1,meta,text,Legend,1,30 chars max you want to match 0,1,meta,text,Legend,,30 chars max then the following regex should suit your needs:

=(.*)b\d{1,2}(.*)

Concatenating the first and second match groups (replace string $1$2) gives you your match.

RegExr demo