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.
Advertisements
Regular Expressions in Lisp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s