Reputation: 11
I am trying to write a sublime syntax for a config file used in one of my programs. Sublime syntax files use regex to perform syntax highlighting. Here is an example:
Database=Something
#Database=SomethingElse
The regex should match the first equal sign, but not the second one. That is: match an equal sign that exists in a line that does not start with #. This is what I have right now:
(?'before'^(?!\#).*(?==))(?'equal')=
The regex generates two groups: before and equal. I want to only match the second group (equal). This has to be done purely in regex and not in any programming language so I'm not sure if that is possible. If you have any suggestions or workarounds let me know. I think the solution might be to use a look behind but I haven't been able to make that work.
Upvotes: 0
Views: 55
Reputation: 110735
I understand Sublime uses the Oniguruma regex engine, so it should support the \K
token, which has the effect of resetting the start of the reported match to the current position in the string and discarding all previously-matched characters. If the the string matches the following regular expression the match will be the equals sign of interest. If there is no match the string begins with '#'
or contains no equals sign.
^[^#][^=]*\K=
The regex engine performs the following operations.
^ : match beginning of line
[^#] : match a character other than '#'
[^=]* : match a character other than '='
\K : reset start of match and discard previous matches
= : match '='
^
could be replaced with \A
to match the beginning of the string. One could use .*?
in place of [^=]*
(?
makes the match non-greedy).
Upvotes: 1