It looks like TextMate only recognizes Perl-style backreferences when doing a regular expression search/replace. (e.g. $1, $2, etc.) I'm used to the style typically found in text editors: \1, \2, etc. Is there any way TextMate can be made to recognize the latter style?
Trevor
Hello Trevor,
You are confusing backreferences and capture groups I think.
Please see here for the Syntax TextMate uses: http://macromates.com/ textmate/manual/regular_expressions#syntax_oniguruma
Backreferences are indeed \1, \2 and are uses in the Find field
Capture Groups are $0, $1, … and are used in the replace field
Soryu.
On Mar 29, 2006, at 10:04 AM, Soryu wrote:
You are confusing backreferences and capture groups I think.
Please see here for the Syntax TextMate uses: http://macromates.com/ textmate/manual/regular_expressions#syntax_oniguruma
Backreferences are indeed \1, \2 and are uses in the Find field
Capture Groups are $0, $1, … and are used in the replace field
Hmm, you're using different terms than what I'm used to. The term "capture group" means, I thought, the part of a regular expression that is enclosed by parentheses. Backreferences are the \1, \2, etc. used to refer to one of these capture groups, either in the regular expression itself or in the replacement string. In some implementations (such as Perl and Oniguruma), the $1, $2, etc. are also backreferences; they're merely a different syntax for the same thing, I believe.
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?
And like I said, this dual-syntax approach is different from other editors I've used. For instance, if you use the PowerFind Pro feature of Nisus Writer Express, it expects the \1, \2, etc. syntax for backreferences in the replacement string. $1, $2, etc. won't work. The same goes for BBEdit.
Trevor
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