M-x all-things-emacs

Quick Tip: re-builder

March 15th, 2007 by Rob Christie · 5 Comments

Have you ever wanted to test out regular expressions in emacs? Until I found out about re-builder I used to run M-x isearch-forward-regexp (also bound to C-M-s) in a buffer until I figured out the expression I needed. This works but just having M-x re-builder in your bag of tricks helps. In the words of the author, Detlev Zundel

When I have to come up with regular expressions that are more complex than simple string matchers, especially if they contain sub expressions, I find myself spending quite some time in the `development cycle’. `re-builder’ aims to shorten this time span so I can get on with the more interesting bits. With it you can have immediate visual feedback about how well the regexp behaves to your expectations on the intended data.

re-builder supports a number of different input forms for the regular expression. The input type can be changed by C-c TAB when you are in this mode.

Read Regexp Syntax in re-builder

re-builder read syntax example

Another nice feature is the highlighting of the different subexpression matches. Xemacs has added perl regular expression support to re-builder as noted on the emacs wiki. I hope this gets added back into GNU emacs.

Tags: quick · tips

5 responses so far ↓

  • 1 Ryan McGeary // Mar 15, 2007 at 1:51 pm

    Update: This post was originally listed as a newbie tip. After some discussion, we decided that Lisp regular expressions, given their unfamiliar syntax, do not fall under the “newbie” category. We moved this into the “quick tip” category instead. Sorry if your feed readers picked up a double post.

  • 2 MikeH // Mar 15, 2007 at 7:35 pm

    Here’s a page that has a nice lisp function that will transform the Lisp regex into a Perl-like regex and put it into your kill ring
    http://www-gatago.com/gnu/emacs/help/20224365.html

    I didn’t write it — I just googled for it. :)

  • 3 piyo // Mar 16, 2007 at 1:21 am

    I decided to write off re-builder because it was emacs lisp regexp language only until you pointed out “perl regexp support”. It requires a perl binary, but the concept seems useful.

    I find re-builder’s syntax modes confusing, perhaps because I’m not familiar with the terminology. It seems the two modes that are important to the beginning user (like me) are “read” and “string”. “string” is the Emacs Lisp regexp that most people are familiar with because it is the same one used in interaction, such as when using isearch-regexp-forward. “read” is the flavor of regexp you write when creating emacs lisp source code.

    Compare: Match "\\\\a2b3c4e5" (with the quotes):
    string: "\\\\\\\\\(\w+\)\([a-z]3\)\([[:alpha:]]4\)\(e[[:digit:]]\)"
    read: \"\\\\\\\\\\\\\\\\\\(\\w+\\)\\([a-z]3\\)\\([[:alpha:]]4\\)\\(e[[:digit:]]\\)\"

    Syntax mode can be Customized with reb-re-syntax, but (unfortunately?), the default is the harder-to-type “read” mode. re-builder supports converting a “string” syntax to “read” automatically, so a quick tip would be to use “string” mode.

    @MikeH:
    I think that it would be more correct to say that the useful lisp function in your link converts “read” syntax to “string”.

  • 4 Rob Christie // Mar 16, 2007 at 1:50 pm

    piyo: Your comment made me realize I had mis-labeled the title of the example image as string syntax. I have corrected the article. Thanks.

  • 5 Where Are The Wise Men? » Blog Archive » Emacs Notes // Mar 20, 2007 at 9:14 pm

    [...] been reading the all-things-emacs blog a lot lately and two of their recent posts talked about packages that I didn’t have in my Emacs.  I looked around and [...]