background:
For syntax highlighting in Sublime Text,
you can write a tmLanguage
file with a corresponding tmTheme
file.
The tmLanguage
file contains regular expressions in which you give names to,
and then the tmTheme
file uses those names to style what was captured.
I want to colorize the same pattern differently depending on how many duplicate patterns came before it. Or, to put it another way, I want to style the nth match of each pattern on each line differently.
the problem:
So for example,
How can I write 3 regular expressions to match the following bold groups?
< foo >< bar >< baz >
< foo >< bar >< baz >
< foo >< bar >< baz >
anything could be inside of < >.
expression 1 would capture the first instance of <*.?>
expression 2 would capture the second instance of <*.?>
expression 3 would capture the third instance of <*.?>
Assume the three examples above are actually the same line.
My goal is to get get each group a different color
<this would be red> <this would be orange> <this would be yellow> <etc..>
The regular expression language is Oniguruma.
My attempts so far:
I can capture the first group like this:
^<.*?>
I can't find out how to capture the second group only
^<.*?>{2} captures nothing
<.*?>{2} captures nothing
<.*?>{2,} captures nothing
^(?:<.*?>)<.*?> captures 1st and 2nd
^(?!<.*?>)<.*?> captures nothing
^(?=<.*?>)(<.*?>) captures 1st
^(?=<.*?>)(<.*?>){1} captures 1st
^(?=<.*?>)(<.*?>){2} captures 1st and 2nd
(?=<.*?>)(<.*?>) captures everything