1

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

Community
  • 1
  • 1
kapsula
  • 75
  • 9
  • 1
    You are asserting either a `:` or `=` to the left followed by repeating matching any char until you encounter b followed by a digit. The regex can not match what comes after b1 because there is no `:` or `=` after it. – The fourth bird Apr 19 '20 at 12:16
  • @The fourth bird, Ahh ok i see.. i will reflect upon a solution, if you or any one could propose a correction, i will be grateful.. – kapsula Apr 19 '20 at 12:23

2 Answers2

3

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

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    Oh man, thanks @The fourth bird, it works :), it's been 3 days know trying to solve this issue.. just to mention i removed the first "^" sign to get exactly what i want, if i keep it, it will match also what's before the equal sign.. – kapsula Apr 19 '20 at 15:13
  • 1
    Done :), i didn't know it.. i'm not aware of the all stackoverflow instructions.. thanks for reminding me @The fourth bird – kapsula Apr 19 '20 at 18:21
0

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

simon-pearson
  • 1,601
  • 8
  • 10
  • Thanks @simon-pearson.. Yes it works for this specific line, but it doesn't for the whole.. how to get : (en-EN) (0,1,meta,text,Legend,,30 chars max) (Contain) i will add an image to clarify my idea.. – kapsula Apr 19 '20 at 13:10