TextMate: Smarter Regular Expression Syntax for Perl

June 14th, 2006

Regular expressions are hard to read. TextMate could probably help by having a syntax for regular expressions, but we’ll keep that for another day. A simple first step though would be to make this:

$var=~s/find_me/replace_with_this/;

easier to read by giving the find_me and the replace_with_this parts their own scope. That is easily done: In TextMate, open the bundle editor, and find the perl syntax definition. Near the beginning of the patterns section you will find this definition for string.regexp.perl.replace:

{   name = 'string.regexp.perl.replace';
    match = '\b(s|tr|y)\s*([^A-Za-z0-9\s])(.*?)(?<!\\)(\\{2})*(\2)(.*?)(?<!\\)(\\{2})*(\2)';
    captures = { 1 = { name = 'keyword.function.perl'; }; };
},

Replace the captures line with this fragment:

captures = 
{   1 = { name = 'keyword.function.perl'; };
    3 = { name = 'string.regexp.perl.replace.search-part'; };
    4 = { name = 'string.regexp.perl.replace.search-part'; };
    6 = { name = 'string.regexp.perl.replace.replace-part'; };
    7 = { name = 'string.regexp.perl.replace.replace-part'; };
};

Now the string.regexp.perl.replace.search-part and string.regexp.perl.replace.replace-part scopes are available for syntax highlighting.

Entry Filed under: OS X, TextMate


Calendar

September 2017
M T W T F S S
« Jun    
 123
45678910
11121314151617
18192021222324
252627282930  

Most Recent Posts