Regular Expressions in Lisp

There are various different regular expression libraries available for Lisp. A nice overview can be found at CLiki. I had a look at the Portable Perl-compatible regular expressions for Common Lisp (CL-PPCRE). Installation is very easy using Quicklisp. After installation you can add the library to your image as follows.

(ql:quickload “cl-ppcre”)

Then you can use the library in your code. Let’s define a very simple function to extract some text from a given string.

(defpackage :mhn-regex
  (:use :common-lisp :cl-ppcre)
  (:export #:print-match))
  
(in-package :mhn-regex)

(defun print-match ()
  (let ((a 0) (b 0))
    (setf (values a b) (cl-ppcre:scan-to-strings ” ([a-z]+) ” “My text string.”))
    (print (aref b 0))))

As you can see, using the library is straightforward in this case. I’ll try to find some free time and create some more examples.

Comment