How do you use more than 9 regex capture groups in emacs?

6

I'm trying to transform a rather long sequence of values to a different format, using emacs' replace-regex command. There are more than 9 values to extract, so the \1 - \9 back references are not enough.

How can I back reference more than 9 capture groups in emacs? Can named groups be used? If so, how?

Erik Öjebo

Posted 2011-05-02T19:40:22.560

Reputation: 215

Answers

7

Since Emacs 23, you can include Lisp code in a regexp replacement text. This gives you a way of using more backreferences. The function match-string returns the numbered backreferences.

\1 … \9 \,(or (match-string 10) "")

Since Emacs 22, if there's any parenthesized group in the regexp that you don't need to have a backreference for, use \(?:…\) (Emacs calls this a shy group).

Gilles 'SO- stop being evil'

Posted 2011-05-02T19:40:22.560

Reputation: 58 319