なんだこれは

はてなダイアリーから移転しました。

cl-irregsexp の 利用メモ

全く関係者ではないのだけれども、あとあとのために cl-irregsexp のメモを作成した。

perl でいう正規表現の前方参照はどうやって書くんだろうか?

情報

インストール

quicklisp にあったので、そのままインストール。
このとき依存関係で alexandria も追加でインストールされた。

(ql:quickload "cl-irregsexp")

if-match-bind

(defmacro if-match-bind (bindings string &optional (then t) (else nil))
  "Perform a match bind, but return ELSE on failure instead of raising match-failed"
;; omitted
)

string が bindingsにマッチしたら、then 部が、そうでなければ else部が返る。
この bindigs が 正規表現にあたるものになる。

(cl-irregsexp:if-match-bind ("a") "abc") ;->T     先頭が a "^a"
(cl-irregsexp:if-match-bind ("b") "abc") ;->NIL   先頭が b "^b"
(cl-irregsexp:if-match-bind (_ "b") "abc") ;-> T  どこかに b "(^.*)b" で $1 が _
(cl-irregsexp:if-match-bind (_ "bc") "abc") ;-> T  どこかに bc "(^.*)bc" $1 が _
(cl-irregsexp:if-match-bind (_ "c") "abc") ;-> T  どこかに c  "(^.*)c"  $1 が _
(cl-irregsexp:if-match-bind ((any (char))) "abc" any) ;->"a" 
(cl-irregsexp:if-match-bind (_ (any (char))) "abc" any) ;->"a"
(cl-irregsexp:if-match-bind ("a" (any (char))) "abc" any) ;-> "b"
(cl-irregsexp:if-match-bind (_ (any (char)) (last)) "abc" any) ;-> "c"

ここで、(char) は *match-bind-function-abbreviations* で定義されている。

略称 *関数名 補足
= literal
- match-element-range
integer match-integer
unsigned-byte match-unsigned-integer
fixnum match-fixnum
float match-float
space match-one-whitespace
last match-end
char eat 任意
string eat

char と string は同じ。なんだかもったいない気がする。

さらに *match-bind-operator-abbreviations* ってので、orなどが定義されている。

演算子 関数名
+ match-one-or-more
* match-zero-or-more
progn progn
:? try-match
not match-not
and match-all
or match-any