正規表現機能別一覧 - lisp-cookbook-ja/common-lisp GitHub Wiki

正規表現

  • 注意
    • Common Lispの文字列の表現では、"\x"と記述した場合は"x"と等価になります。

このためバックスラッシュを含む表現の場合、バックスラッシュ自体をエスケープして"\x"と記述する必要があります。

cl-ppcre ACL regexp2 Perl (5.8)
任意の1文字 . . .
いずれか1文字 […] […] […]
いずれか以外の1文字 [^…] [^…] [^…]
エスケープ |||
選択 r1 r2 r1
一回または零回(欲張り) ? ? ?
零回以上(欲張り) * * *
一回以上(欲張り) + + +
n回以上m回以下(欲張り) {n,m} {n,m} {n,m}
n回以上(欲張り) {n,} {n,} {n,}
零回以上n回以下(欲張り) {,n} {,n} {,n}
n回(欲張り) {n} {n} {n}
一回または零回(ものぐさ) ?? ?? ??
零回以上(ものぐさ) *? *? *?
一回以上(ものぐさ) +? +? +?
n回以上m回以下(ものぐさ) {n,m}? {n,m}? {n,m}?
n回以上(ものぐさ) {n,}? {n,}? {n,}?
零回以上n回以下(ものぐさ) {,n}? {,n}? {,n}?
行先頭 ^ ^ ^
行末尾 $ $ $
単語先頭 なし なし なし
単語末尾 なし なし なし
単語境界 \b \b \b
非単語境界 \B \B \B
文字列先頭 \A \A \A
文字列末尾、または文字列末尾の改行の直前 \Z \Z \Z
文字列末尾 \z \z \z
グループ ('''regex''') ('''regex''') ('''regex''')
グループ(キャプチャ無し) (?:'''regex''') (?:'''regex''') (?:'''regex''')
肯定先読み/後方一致指定 (?='''regex''') (?='''regex''') (?='''regex''')
否定先読み (?!'''regex''') (?!'''regex''') (?!'''regex''')
肯定戻り読み (?<='''regex''') (?<='''regex''') (?<='''regex''')
否定戻り読み (?<!'''regex''') (?<!'''regex''') (?<!'''regex''')
バックトラック禁止 (?>式) (?>式) (?>式)
条件付き展開 (?(?cond)true false) (?(?cond)true
名前付きキャプチャ (?式) (?式) (?式)
名前付きキャプチャ参照 \k ※1 \k \k
キャプチャ参照 \1 \2 \3 ... \1 \2 \3 ... \1 \2 \3 ...
正規表現内のスペースを無視する (?x:'''regex''') (?x:'''regex''') (?x:'''regex''')
「.」を改行にマッチさせる~%(シングルラインモード) (?s:'''regex''') (?s:'''regex''') (?s:'''regex''')
「^」と「$」を行の先頭と末尾にマッチさせる~%(マルチラインモード) (?m:'''regex''') (?m:'''regex''') (?m:'''regex''')
大文字と小文字を区別しない (?i:'''regex''') (?i:'''regex''') (?i:'''regex''')
正規表現内のスペースを無視する (?x:'''regex''') ※2 (?x:'''regex''') (?x:'''regex''')
水平タブ \t \t \t
垂直タブ なし なし \v
改行 \n \n \n
復帰 \r \r \r
バックスペース \b \b \b
改ページ \f \f \f
ベル \a \a \a
エスケープ \e \e \e
制御文字表現 \c \c \c
8進数表現 \nnn \nnn \nnn
16進数表現 \xHH \xHH \xHH
単語構成文字 \w \w \w
非単語構成文字 \W \W \W
空白文字 \s \s \s
非空白文字 \S \S \S
10進数字 \d \d \d
非10進数字 \D \D \D
16進数字 なし \h \h
非16進数字 なし \H \H
8進数字 なし なし なし
非8進数字 なし なし なし
Unicodeプロパティによる文字クラス指定 \p ※3 なし \p
Unicodeプロパティによる文字クラス指定の否定 \P ※3 なし \P
正規表現文字のエスケープ \Q'''regex'''\E ※4 なし \Q'''regex'''\E

※1 cl-ppcre:allow-named-registers を非nilに設定した場合、正規表現文字列の中だけで使える

(let* ((ppcre:*allow-named-registers* T)
       (re (ppcre:create-scanner "(?<quux>foo)\\k<quux>"))) ;コンパイルする
  (ppcre:scan-to-strings re "barfoofoobar"))
;=>  "foofoo"
;    #("foo")
;;; allegro cl regexp2
(replace-re  "bazfoofoobar"
             "(?<baz>.*)(?<foo>foo)\\k<foo>(?<bar>.*)"
             "\\k<foo>,\\k<bar>,\\k<baz>,quux")
;=>  "foo,bar,baz,quux"


(let* ((ppcre:*allow-named-registers* 'T)
       (re (ppcre:create-scanner "(?<baz>.*)(?<foo>foo)\\k<foo>(?<bar>.*)")))
  (ppcre:regex-replace re 
                       "bazfoofoobar" 
                       "\\k<foo>,\\k<bar>,\\k<baz>,quux"))
;=>  "\\k<foo>,\\k<bar>,\\k<baz>,quux"
;    T

;;; 参考:番号での参照
(let* ((ppcre:*allow-named-registers* 'T)
       (re (ppcre:create-scanner "(.*)(foo)\\2(.*)")))
  (ppcre:regex-replace re 
                       "bazfoofoobar" 
                       "\\2,\\3,\\1,quux"))
;=>  "foo,bar,baz,quux"
;    T

※2 コメントは、『;』ではなくPerl互換の『#』で開始

(ppcre:scan-to-strings "(?x:
foo  #ふー
bar  #ばー
baz  #ばず
)" "foobarbaz")
;=>  "foobarbaz"
;    #()

※3 cl-ppcre-unicodeを導入することにより利用可能

(ql:quickload :cl-ppcre-unicode)

(ppcre:scan "\\p{Letter}+" "あaABc123")
;=>  0
;    5
;    #()
;    #()

※4 cl-ppcre:allow-quotingを非nilにした場合に利用可能

(let* ((ppcre:*allow-quoting* t)
       (re (ppcre:create-scanner "\\Q.txt\\E")))
  (values (ppcre:scan-to-strings re ".txt")
          (ppcre:scan-to-strings re "otxt")))
;=>  ".txt"
;    NIL
⚠️ **GitHub.com Fallback** ⚠️