sheriffderek
sheriffderek

Reputation: 9053

Persistent Regex Highlighting with Sublime Text 2

I would like to have a special "comment" syntax with a special highlight.

For example:

I'm using Sublime Text 2. I found this Persistent Regex Highlight that seems to do exactly that, but I can't seem to figure out how to say....

" highlight anything between <!--x   and   x--> "

So far, I can get this basic string, "yo" to highlight, and I've tried a bunch of regex patterns but alas, I can't seen to get it! (haven't used regex too much and my search terms aren't specific enought)

{
    "regex": [{
        "pattern": "yo",
        "color": "ff0000",
        "ignore_case": false
    }]
}


This worked... but I don't understand it.

{
    "regex": [{
        "pattern": "(?<=<!--x).*?(?=x-->)",
        "color": "ff0000",
        "ignore_case": false
    }]
}

I will appreciate any direction. - @sheriffderek

https://github.com/skuroda/PersistentRegexHighlight

Upvotes: 0

Views: 810

Answers (1)

cobbal
cobbal

Reputation: 70795

So, it seems like you have a solution that works, but you're looking for an explanation? (If I'm misunderstanding the question here, please correct me).

Let's break down the regular expression into its components:

(?<=    <!--x    )    .    *?    (?=    x-->    )
  • <!--x and x--> are literal strings, that appear directly in the text.

  • (?<= subpattern) is known as a lookbehind assertion. It requires that the text that is matched is preceded with subpattern, but it doesn't include the subpattern in the returned match text. In this case, it means that <!--x must occur before any highlighted text.

  • (?= subpattern) is similarly known as a lookahead assertion. Exact same thing, it requires that the subpattern go after the match, but not be included in it.
  • . matches any character besides newline (which may or may not be what you want
  • *? is a non greedy repeat, which means it will match any number of the pattern before it (. in this case), but that if it has multiple choices of what to match will pick the shortest one that works.

Upvotes: 1

Related Questions