diary at Telent Netowrks

cl-ppcre is a portable CL#

Sun, 24 Aug 2003 07:16:35 +0000

cl-ppcre is a portable CL package providing perl-compatible regexps. There are two things I'd like to say about it on the basis of ten minutes use

  • Although billed as perl-compatible, that doesn't mean you have to use the Perl syntax: you can also write regexes as trees. For example

    (defparameter postcode-p
      (let ((a '(:CHAR-CLASS (:RANGE #\a #\z)))
    	(n :digit-class)
    	(s '(:GREEDY-REPETITION 0 NIL :WHITESPACE-CHAR-CLASS)))
        (cl-ppcre:create-scanner
         `(:alternation
           (:sequence ,a ,n       ,s  ,N ,A ,A)
           (:sequence ,A ,N ,N    ,s  ,N ,A ,A)
           (:sequence ,A ,A ,N    ,s  ,N ,A ,A)
           (:sequence ,A ,A ,N ,N ,s  ,N ,A ,A)
           (:sequence ,A ,N ,A    ,s  ,N ,A ,A)
           (:sequence ,A ,A ,N ,A ,s  ,N ,A ,A)))))

    • (cl-ppcre:scan postcode-p "w1y 1ha") 0 7
    • ()
    • ()
  • That's a regex for UK postcodes (actually, not quite. I should add the special case for GIR 0AA, which apparently is Alliance & Leicester Girobank Plc in Bootle). Isn't it just much easier to see what it does as a tree?