Emacs regular expressions...

Posted on January 17, 2011 by phimuemue

Some time ago, I had to create a LaTeX/beamer presentation. Suddenly, I realized that all overlays were shifted, i.e. I wanted <3> to become <5> and <7> to become <9> (I wanted all overlays two slides later.) See how emacs' regex engine did this in seconds. Emacs has a quite powerful regular expression engine that supports even embedded emacs lisp! So I did the following:

M-x replace-regexp RET <\([0-9]*\)> RET <\,(+ 2 (string-to-int \1))>

This means the following: Replace every occurence of something of the form <"any number"> and capture the number inside. The part \,(+ 2 (string-to-int \1)) converts the captured number (which is captured as a string) to an int and adds 2 to it. \, indicates that the following is an emacs lisp part. Then (+ 2 (string-to-int \1)) is computed. It takes the captured number (\1) and adds 2.

I usually do not post such things but I was that dazzled that I just had to share this.

Edit: When I read of this functionality for the first time, I thought that no one would ever use such a feature, but I came to use this thing several times already!