[TxMt] Help with RegEx in Bundles

Allan Odgaard allan at macromates.com
Thu Jun 16 17:40:47 UTC 2005


On Jun 16, 2005, at 19:25, Edmundo Ortega wrote:

> I want to color: "addItem(" and ")" but not "name: String, item:  
> MenuItem
>
> Is this possible in one regex expression?

Yes, assuming you're using 1.1b12 you can name the captures, this is  
done like this:
     match = "(\\b(function)\\s+()[a-zA-Z_]\\w*)\\s*\\([^\\)]*(\\))";
     captures = {
         1 = { name = "storage.type"; };
         3 = { name = "storage.type"; };
     };

That way capture #1 and #3 gets “storage.type” as the name.

> Is there some way to match something by looking for a leading (or  
> trailing) indicator without including the indicator in the match?

You can both use look-ahead and look-behind assertions in the regular  
expression.

E.g.: match = "foo(?=bar); or
       match = "(?<=foo)bar;

The first one matches foo, when followed by bar. The second matches  
bar, when preceded by foo.

> I'm currently using this regex to successfully capture the whole  
> thing:
> match = "\\b(function)\\s+([a-zA-Z_]\\w*)\\s*\\([^\\)]*\\)";

Another approach is to use begin/end matches. E.g.:
     begin = "\\b(function)\\s+([a-zA-Z_]\\w*)\\s*\\(";
     end = "\\)";

You'd still need to put a capture around these patterns and name that  
capture (to get a name associated only to these two things), but it  
allows you to provide a new set of rules for the stuff between the  
begin/end patterns using e.g.:
     patterns = (
         { name = "support.class"; match = "\\b(String|MenuItem|...)\ 
\b"; },
         ...
     );




More information about the textmate mailing list