Common Lisp の正規表現パッケージ CL-PPCRE を使ってみた。
処理系は CLISP
;; quicklisp で "ppcre" を検索 > (ql:system-apropos "ppcre") #<SYSTEM arnesi+.cl-ppcre-extras / arnesi+-20120909-darcs / quicklisp 2013-04-20> #<SYSTEM arnesi.cl-ppcre-extras / arnesi-20101006-darcs / quicklisp 2013-04-20> #<SYSTEM cl-ppcre / cl-ppcre-2.0.4 / quicklisp 2013-04-20> #<SYSTEM cl-ppcre-template / cl-unification-20130128-cvs / quicklisp 2013-04-20> #<SYSTEM cl-ppcre-test / cl-ppcre-2.0.4 / quicklisp 2013-04-20> #<SYSTEM cl-ppcre-unicode / cl-ppcre-2.0.4 / quicklisp 2013-04-20> #<SYSTEM optima.ppcre / optima-20130420-git / quicklisp 2013-04-20> #<SYSTEM parser-combinators-cl-ppcre / cl-parser-combinators-20121125-git / quicklisp 2013-04-20>
;; CL-PPCRE を load > (ql:quickload :cl-ppcre) To load "cl-ppcre": Load 1 ASDF system: cl-ppcre ; Loading "cl-ppcre"
(:CL-PPCRE)
;; Sample 文字列 > (defvar user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_6) AppleWebKit/534.28 (KHTML, like Gecko) Chrome/12.0.728.0 Safari/534.28")
;; マッチした部分文字列 > (ppcre:scan-to-strings "M\\S+" user-agent) "Mozilla/5.0" ;
;; グローバルマッチ > (ppcre:all-matches-as-strings "M\\S+" user-agent) ("Mozilla/5.0" "Macintosh;" "Mac" "ML,")
;; こんな関数を作っておくと直感的? (defun m/re/g (re str) (ppcre:all-matches-as-strings re str))
> (m/re/g "\\d+" user-agent) ("5" "0" "10" "6" "6" "534" "28" "12" "0" "728" "0" "534" "28")
まだまだこれから調べる
参考サイト:
CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp
#CommonLisp #Lisp #regex #CL-PPCRE #Quicklisp #CLISP
SN 2013/07/20 22:55:01
Archives > CommonLisp_CL-PPCRE_01.html