4
votes

When I use the regexp-builder, I need to escape things in a different way from the way I do it when using replace-regexp. Now, this thread explains that these two commands use a different syntax, but why is that?

Also, I went through this blog post: Re-builder: The Interactive Regexp Builder, and I added

(require 're-builder) 
(setq reb-re-syntax 'string)

to my .emacs file following the advice on the site. However, I still need to type " around my regexp to make it work. I thought changing the syntax language would take care of this but it doesn't.

With this, my actual questions are:

  1. Is it sill the case that Emacs does not support PCRE? Are there any workarounds to this?
  2. Once I have the right regexp in regex-builder, is there any way to directly send the regexp to replace-regexp and enter the replacement string?
3
@devnull Thanks! I just fixed that.Amelio Vazquez-Reina
This might interest you.devnull

3 Answers

3
votes

There's a package in the MELPA repository called pcre2el that adds PCRE support to many parts of Emacs, including regexp-builder and replace-regexp.

2
votes

Regarding question #2: No (at least not by default), but there's another way to do that without using re-builder.

  • Start by doing a regexp isearch for your pattern. Because it's an isearch, you'll see the matches interactively, a bit like re-builder (albeit without coloured groupings).

  • Still in isearch, once you're happy with the pattern, type C-M-% to call isearch-query-replace-regexp which will prompt you for the replacement.

You can of course simply copy your re-builder string from its buffer and yank it as a replacement string (but that's undoubtedly not news).

I was curious about the need for quotes in re-builder with string syntax. It seems that's it's just a formality of the system, and reb-read-regexp returns everything between the first and last " when using that syntax. Maybe it's intended to ensure that leading or trailing whitespace can't confuse matters -- re-builder does use leading whitespace for improved visibility, and trailing whitespace would be harder to spot. Or maybe it just made some of the code more convenient/consistent.

1
votes
  1. No, Emacs doesn't support PCRE, and as far as I know there is no work-around for that.
  2. I don't think so.

To answer your first question, why does re-builder use a different syntax than replace-regexp:

By default, re-builder uses the syntax that is appropriate for writing elisp programs. In the context of a written program, regexps are entered within strings. Inside a string, backslashes have a special meaning which conflicts with using the backslash as part of a regexp. Consequently, within a string, you need to double a backslash to use it to signify part of the regexp syntax.

replace-regexp, on the other hand, is designed to be used interactively by the user, and it explicitly expects the input to be a regexp. As a convenience, it interprets backslashes as regexp syntax, not as string escapes. Which is why you can use single backslashes in this context.