Ads by Google

Thursday, October 15, 2009

Using Emacs Regular Expression Builder

Sometimes it's worth using or getting help on the work you're trying to do. For example, in the Unix world, regular expressions are powerful tools to match,print, find or delete text strings. While mostly the regular expressions achieve what we want, it might be good to get some help in identifying whether the regex is doing what we intend it to do.

In Emacs, there is such a feature called re-builder, invoked as M-x re-builder. As you type the regular expression in the minibuffer like prompt within the double quotes, the text buffer gets highlighted with the current regex that has been typed so far. As you keep modifying the regex, the buffer too is highlighted dynamically on what that regex will match. Once you're satisfied with the regex C-c C-w will copy it to the kill ring and use it in, say C-M % which is generally bound to query-replace-regexp.

The following screenshot shows the matched text and the regex entered. I was trying to delete off everything after the 2nd semicolon.

The highlighted portion is due to the regex shown below

Give it a shot. It saves you a lot of time figuring out the correct regex.


EOL (Eric O LEBIGOT) said...

Thank you for sharing this! I did not know about re-builder!

I would like to add that C-c C-q quits the re-builder buffer when one is done, and that the usual C-h m gives other useful key bindings.

Mikhail Glushenkov said...

BTW, there is also re-builder-x, which gives you Perl syntax.

Anonymous said...

Hello from Russia!
Can I quote a post "No teme" in your blog with the link to you?

sivaram said...


Go ahead. Do let me know the link.