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.