Archives

Creative Commons License
This blog is licensed under a Creative Commons License.

A regular expression IDE for Emacs

| 17 Comments | No TrackBacks

I’ve started work on a new mode for Emacs, which intends to be something like an “IDE” for regular expressions. There are similar tools out there, such as [Reggy][] for OS X and [The Regex Coach][] for Windows. But the former doesn’t show me subgroup matches, and the latter won’t run on OS X. Which made me wonder, wouldn’t this be easy to do in Emacs? Three hours later says yes.

After downloading regex-tool.el, put it somewhere where your Emacs can see it (typically /usr/local/share/emacs/site-lisp), and then add this to your .emacs file:

(load "regex-tool" t)    ; load regex-tool if it's available

Then type M-x load-file and load it manually for your current session. Once that is done, type M-x regex-tool to start the show. It will create a new Emacs frame for you looking something like this:

regex-tool.png

The *Regex* section starts out blank, awaiting you to type in your regular expression. The default syntax is Emacs; if you would prefer regex-tool to use full Perl regular expressions, type the command M-x customize-group, choose the group regex-tool, and then change the backend to “perl” by clicking on the Value Menu. When configured to use Perl, regex-tool will actually invoke a real Perl subprocess to run your matches against, so you can use whatever syntax your installed version of Perl accepts.

The matched parts of the sample string are highlighted in bold red, and all the matching subgroups (including group 0, to show the whole match) are indicated in the *Groups* buffer.

I’m sure there are many ways this little tool could be extended, so please don’t hesitate to send me a note with your suggestions. Or find me in the #emacs IRC channel, on the server irc.freenode.net. I’m johnw on there, and am online most evenings.

No TrackBacks

TrackBack URL: http://www.newartisans.com/mt/mt-tb.cgi/31

17 Comments

It was accidentally deleted by a nightly upload script. It is back now.

Installed. Thank you so much!

*Group* is a bad buffer name, it conflicts with Gnus. :(

Oh it’s *Groups*. Duh. Sorry.

Very nice work, especially with the perl integration.

(BTW, for a good OSX regex explorer, look at RegExhibit.)

sorry if I am missing the point. how is it different from good old re-builder:

http://cvs.savannah.gnu.org/viewvc/checkout/emacs/emacs/lisp/emacs-lisp/re-builder.el

Nice tools. But I now have a bug report for it :) It will enter into the dead loop if only typing “^” in the Regex buffer, since (re-search-forward “^” nil t) will not move the point forward….

Same problem when a single “$” as well.

Thanks, I’ll fix those right away Brian.

Yaroslav: re-builder is for making Emacs regexps which are intended to be part of a string. Only. Mine can be used for testing Perl regexps that are used in a // expression in Perl, for example.

Thanks for this tool, it works like it should on Windows. I like it enough to cart it around. Too bad I don’t have perl installed.

I modified my copy so that it doesn’t call the external process on every keypress, group 0 character position, and added “support” for ruby or any other callable executable. If you want to take a look at it I can send it to you.

BTW, is the defgroup definition correct?

Having not much need for regexp in my current interests I can’t think of any other things I want except for Java regex support, a cheatsheet (ah heck just use yer browser), a “busy” status display, support for regex mode setting (instead of just /mg), regex flavor displayed in the statusbar, and more (defcustom)s.

How to avoid the slow external startup? (Ruby, Java) Make a regex server and do some IPC?

I’d love to see your changes, piyo. I want to add a good extensions mechanism for adding in custom backends and switching on the fly. Also, a cheat sheet is not a bad idea!

I’m using Xemacs 21.4.20 on an Ubuntu Gutsy box. When I try to load the file (or type M-x regex-tool on the minibuffer) it complains about some functions:

Compiling file /usr/local/share/emacs/site-lisp/regex-tool.el at Wed Mar 26 16:02:26 2008
While compiling the end of the data:
  ** The following functions are not known to be defined: 
    make-frame-command, remove-overlays, overlay-put, make-overlay

I’ve tried to find documentation make-frame-command as a function but my elisp has none.

What have I done wrong? What can I do to work it out?

Thanks, Nacho

You’ve done nothing wrong, I just never tested regex-tool on XEmacs. For example, XEmacs uses “extents” instead of “overlays”. The code shouldn’t take long to port, but I’m afraid it will have to be an XEmacs expert who does so; I never use it.

Is it possible to add flags and enter the expression like this?

/mystring/si

Hi Thomas, at present no.

This sounds like the answer to my prayers.

I can’t seem to download it, though. The link is broken.

Leave a comment

About this Entry

This page contains a single entry by John Wiegley published on October 29, 2007 2:48 AM.

Script of the week: sizes was the previous entry in this blog.

Script of the week: bzdmg is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Recent Comments

  • Przemek: Great article, indeed. @Kyle: I’m a git newbie and SVN read more
  • Kyle Bennett: John, thanks for the work you put into this. Since read more
  • Sigi: You deserve a lot of praise for this article. It’s read more
  • Tony: this is an excellent write up; I’ve been reading much read more
  • John Wiegley: Thanks for letting me know, I’ll try to rectify the read more
  • Rudi Farkas: Hello John Above, you say “The date at the front read more
  • Uwe Kleine-König: Hi John, a comment to the paragraph about reset: $ read more
  • Leonardo Boiko: Thank you very much for this; as a bottom-up guy read more
  • John Wiegley: Thanks for the update. I’ll include this among the next read more
  • Laust Rud: Excellent writing, thanks! The url for the git-core tutorial has read more
OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.25