28

I have an emacs buffer containing the text

a1b2c3

using the regexp-builder, i create the regexp

"b\\(2\\)"

and can see the match highlighting (b2, with the 2 in a different colour).

however, when i paste the expression into replace-regexp, i get 0 matches. both with and without the quotes. to get a match i need to use

b\(2\)

i guess there's some escaping going on here, but surely i must be doing something wrong. having to manually escape seems to defeat half the point.

second
  • 28,029
  • 7
  • 75
  • 76

1 Answers1

33

regexp-builder can show the regexp in several syntaxes. By default, it uses the syntax that is appropriate to put in an Emacs Lisp source (read syntax), so the regexp is placed between double quotes and every backslash is doubled. For interactive use, select the string syntax with C-c C-i (reb-change-syntax).

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • 3
    I'm really confused by this answer ; Consider this: `string[5string]' now regexp-builder matches the "5" ("any number preceded by an opening angle bracket") with "\\[[0-9]". Now if I `M-x query-replace \\[[0-9] RET plop' I get nothing. I tried \\[\[0-9\] (works in builder too), nothing. You did not indicate where I'm supposed to type `C-c C-i' : in the regexp-builder, in the minibuffer while using it, elsewhere? Turned out it was in the regexp-builder, but then it offers me 4 choices (read, rx, sregex, string) which one should I choose **so that emacs understands it's own regexp syntax?** – yPhil Aug 17 '12 at 12:32
  • Wow the comment syntax escapes the escape chars, that will make commenting about the escape chars uneasy :) Seriously, even in "code" markers, escaping? – yPhil Aug 17 '12 at 12:38
  • @PhilippeCM I'm a bit confused by your comment, and it's not just because of the missing backslashes. If you're typing a regexp interactively, pick `string`. If you mean to insert the regexp in a Lisp snippet, pick `read`, which writes a Lisp string literal with `\\ ` and `"` escaped. – Gilles 'SO- stop being evil' Aug 17 '12 at 17:00
  • 6
    At least in Emacs 24.2, picking `string` doesn't change what gets copied by `C-c C-w`. You need manually select the expression between quotes to get a version usable in `replace-regexp`. – Phil Calvin Sep 17 '13 at 17:17
  • 1
    I'm looking for functionality for the work flow of starting with regexp-builder, build the desired regular expression interactively, (trial-and-error), after reached the desired one, issue a command to reuse the built regex expression to do replace-regex in emacs. Please share if it's already possible? or Some alternative? Thanks, – Yu Shen Apr 04 '15 at 00:31
  • @YuShen Sounds like a question you could ask on [emacs.se]. – Gilles 'SO- stop being evil' Apr 04 '15 at 00:54
  • 1
    @YuShen did you happen to ever find or implement that workflow? if not I may try to. – scry Dec 04 '21 at 17:50
  • @scry No, I haven't done it. It would be super, if you could. Thanks in advance! – Yu Shen Dec 05 '21 at 17:49
  • My workflow for a default emacs 26.1 install: Run `re-builder` via `M-x re-builder`. Build regular expression within the double quotes (`" "`) provided; escaped characters like parentheses and curly braces require double backslashes. Once the expression is working in `re-builder` and you want to run it in `query-replace-regexp`, first copy ONLY the text within the double quotes; then, paste it into a throwaway buffer so you can replace the double backslashes with single backslashes; now, paste the result into `query-replace-regexp` (`C-M-s` or `M-x query-replace-regexp`). – baltakatei Mar 09 '22 at 13:34