Skip to content

re-builder: the Interactive regexp builder

by mickey on April 12th, 2011

I doubt it’s a well-kept secret that Emacs has a regexp “helper” called M-x re-builder. But if you haven’t heard about it before, Emacs’s re-builder lets you interactively build a regular expression and see what it matches on the screen. It’ll even uniquely color capturing groups so you can tell them apart.

Syntax

What most people don’t know is re-builder’s support for different syntax; but, sadly, not PCRE — sorry!

There are five different syntax choices (see table below). You can either use customize (M-x customize-variable RET reb-re-syntax RET) or set the variable (reb-re-syntax) directly.

Syntax Description
read default. Similar to string but requires “double escaping” of backslashes like you would be required to do in elisp. Example: “\\(foo\\|bar\\)”
string recommended. Similar to read but you don’t have the issue of backslash plague that haunts the default settings. Example: “\(foo\|bar\)”
sregex A symbolic regular expression engine that uses s-expressions instead of strings.
lisp-re Yet another regular expression engine that uses s-expressions
rx A third, and far more advanced, s-expression regexp engine. Use this and not sregex or lisp-re if you want to use a lisp-style regexp engine.

The backslash madness

I recommend you switch to string right away; there’s little reason to use read, and the extra escaping will drive you insane unless you’re used to writing regexp in elisp.

Add this to your .emacs to switch the default syntax to string:

Useful Keybinds

If you do write a lot of elisp, you probably use (or should use!) rx to make your regexp experience in Emacs a bit more pleasant. Unfortunately, you can only have one default setting at a time so you have to switch manually with C-c TAB in re-builder.

You can enter the sub-expression mode with C-c C-e to only highlight capturing groups; you can toggle the case sensitivity with C-c C-i; and you can move between matches with C-c C-s and C-c C-r.

The re-builder keybind C-c C-w bears mention as well: it will copy (and convert, where applicable) the expression to a string format suitable for use in elisp.

5 Comments
  1. One thing that I have always wanted to do with re-builder is execute a regexp replace directly from the re-builder against the target buffer. I have not found a simple way to do it, and have been too lazy to implement it myself.

    • I second that motion, it would be a great addition that I will be putting on my todo list and hopefully get around to soon.

  2. Your post i a few days late, i really could have used this the other day ;)

    Thanks for this tip, i have always wanted this kind and wondered if i should write it myself in python (my emacs-lisp is best described as non-existent), but now you tell me it is built-in! Terrific!

  3. onano permalink

    According to these .emacs tips [1], you should not use require or load in your .emacs but autoload and eval-after-load instead.

    [1]: http://cheat.errtheblog.com/s/emacs_tips/

  4. Patrick Maslen permalink

    Thanks for telling me how to disable the extra backslashes. I was just copying the regex to another buffer and removing them manually before.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS