Building Regular Expressions with Emacs

Wouldn’t it be nice to have an environment in which to see the effect of a regular expression on a set of target strings? Ideally such that changes in matching immediately reflected, as one changes and experiments with the regular expression itself? The re-builder functionality built into Emacs does exactly that.

It’s very simple. Start the process, from any buffer, using M-x re-builder. The buffer becomes the “target”, and a prompt at which to formulate the regular expression is shown in a new window at the bottom of the frame.

Screenshot

Matches of the regular expression are shown continuously in the target buffer, and are updated as the regular expression is edited.

It’s that simple.

The description available using the usual C-h f help system is a bit sparse, but much more detail is available in the actual source file (which is accessible, easily, from C-h f re-builder). A few of the most useful keybindings include:

`C-c C-b`        ;; Change the target buffer.
`C-c C-w`        ;; Copy the regular expression to the kill ring.
`C-c C-q`        ;; Quit.

It is also possible to customize the highlighting color and some other details using M-x customize-group.

It is surprisingly neat and simple; I wish I had discovered this earlier.