[TxMt] Backreferences in regexp search/replace

Allan Odgaard throw-away-1 at macromates.com
Sun Apr 2 13:53:17 UTC 2006


On 2/4/2006, at 10:09, Trevor Harmon wrote:

> [...] What confuses me is why Oniguruma has two separate syntaxes  
> for backreferences. If \1, \2, etc. are backreferences to a capture  
> group (using my terms here) within the regular expression, then why  
> use a different syntax for the replacement string?

The replacement string syntax [1] has $variables where variable 1-n  
stem from captured groups from the regular expression’s find string.  
But that is basically all they share. So it is equivalent to the  
following in perl:

    print "$1" if /f(.)o/;

The variable syntax is extensible and opens up for variable  
modifications like with snippets, e.g. ${1:-default}, ${1/regexp/ 
format/}, etc. and it will allow named captures to appear simply as  
$name or ${name} instead of \k<name> (and again, with the ability to  
do some additional testing/formatting of the variable).

In retrospect, the conditional insertion in format strings should  
have used the dollar-notation, and I probably will deprecate the  
current notation, when it becomes possible to do with dollar-notation.

While I may add \1-n for backwards compatibility, it is IMHO a legacy  
notation, and \1 is generally seen as ^A. That \1 was chosen for back- 
references was (afaik) because it was the best way to extend the  
syntax w/o causing existing stuff to break (back-references were  
added later). That it was then carried over to the format string, was  
probably just because no-one gave it any thoughts.


[1] http://macromates.com/textmate/manual/ 
regular_expressions#replacement_string_syntax_format_strings




More information about the textmate mailing list