Praetorian
Praetorian

Reputation: 109149

regex matching pair of brackets

I'm trying to write a Sublime Text 2 syntax highlighter for Simulink's Target Language Compiler (TLC) files. This is a scripting language for auto-generating code. In TLC, the syntax to expand the contents of a token (similar to dereferencing a pointer in C or C++) is

%<token>

The regular expression I wrote to match this is

%<.+?>

This works for most cases, but fails for the following statement

%<LibAddToCommonIncludes("<string.h>")>

Modifying the regular expression to greedy fixes this if the statement is by itself on a line, but fails in several other cases. So that is not an option.

For that line, the highlighting stops at the first > instead of the second. How can I modify the regular expression to handle this case?

It'd be great if there was a general expression that could handle any number of nested <> pairs; for example

%<...<...>...<...<...>...>...>

where the dots are optional characters. The entire expression above should be a single match.

Upvotes: 2

Views: 3130

Answers (1)

Shyamal Pandya
Shyamal Pandya

Reputation: 652

A generic way through regular expressions is difficult -as explained very well in this thread.

You can try to specifically match 2 < characters through a regex. Something like %<.+?<.+?>.+?>.

Upvotes: 2

Related Questions