27

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.

1 Answer 1

32

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).

Sign up to request clarification or add additional context in comments.

9 Comments

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?
Wow the comment syntax escapes the escape chars, that will make commenting about the escape chars uneasy :) Seriously, even in "code" markers, escaping?
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.
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,
@YuShen did you happen to ever find or implement that workflow? if not I may try to.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.