How to replace "(" with "\(" in the regexp, Emacs/elisp flavor?

Posted by polyglot on Stack Overflow See other posts from Stack Overflow or by polyglot
Published on 2010-05-12T03:08:06Z Indexed on 2010/05/12 3:14 UTC
Read the original article Hit count: 304

Filed under:
|

Question as title.

More specifically, I'm rather tired of having to type \(, etc. every time I want a parenthesis in Emacs's (interactive) regexp functions (not to mention the \\( in code). So I wrote something like

(defadvice query-replace-regexp (before my-query-replace-regexp activate)
   (ad-set-arg 0 (replace-regexp-in-string "(" "\\\\(" (ad-get-arg 0)))
   (ad-set-arg 0 (replace-regexp-in-string ")" "\\\\)" (ad-get-arg 0)))))

in hope that I can conveniently forget about emacs's idiosyncrasy in regexp during "interaction mode". Except I cannot get the regexp right...

(replace-regexp-in-string "(" "\\\\(" "(abc")

gives \\(abc instead of the wanted \(abc. Other variations on the number of slashes just gives errors. Thoughts?

Since I started questioning, might as well ask another one: since lisp code is not supposed to use interactive functions, advicing query-replace-regexp should be okay, am I correct?

© Stack Overflow or respective owner

Related posts about emacs

Related posts about elisp