[TxMt] Backreferences in regexp search/replace

Trevor Harmon trevor at vocaro.com
Sun Apr 2 08:09:54 UTC 2006


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2363 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20060402/62324c0f/attachment.p7s>


More information about the textmate mailing list