Skip to content

miyamuko/xl-ppcre

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xl-ppcre - Portable Perl-compatible regular expressions for xyzzy Lisp

SYNOPSIS

(require "xl-ppcre")

(ppcre:scan "(a)*b" "xaaabd")
;=> 1
;   5
;   #(3)
;   #(4)

(ppcre:scan-to-strings "(([^b])*)b" "aaabd")
;=> "aaab"
;   #("aaa" "a")

(ppcre:register-groups-bind (fname lname (#'parse-integer date month year))
    ("(\\w+)\\s+(\\w+)\\s+(\\d{1,2})\\.(\\d{1,2})\\.(\\d{4})" "Frank Zappa 21.12.1940")
  (list fname lname (encode-universal-time 0 0 0 date month year 0)))
;=> ("Frank" "Zappa" 1292889600)

(ppcre:do-scans (match-start match-end reg-starts reg-ends
                             "(\\w)(\\w+)" "foo bar baz")
  (format t "~S~%" (list match-start match-end reg-starts reg-ends)))
;=> (0 3 #(0 1) #(1 3))
;   (4 7 #(4 5) #(5 7))
;   (8 11 #(8 9) #(9 11))
;   nil

(ppcre:do-matches (match-start match-end "\\w+" "foo bar baz")
  (format t "~S~%" (list match-start match-end)))
;=> (0 3)
;   (4 7)
;   (8 11)
;   nil

(ppcre:do-matches-as-strings (match-var "\\w+" "foo bar baz")
  (format t "~S~%" match-var))
;=> "foo"
;   "bar"
;   "baz"
;   nil

(let (result)
  (ppcre:do-register-groups ((#'parse-integer n) (#'intern sign) whitespace)
      ("(\\d+)|(\\+|-|\\*|/)|(\\s+)" "12*15 - 42/3")
    (unless whitespace
      (push (or n sign) result)))
  result)
;=> (3 / 42 - 15 * 12)

(ppcre:all-matches "\\w+" "foo bar baz")
;=> (0 3 4 7 8 11)

(ppcre:all-matches-as-strings "\\w*" "foo bar baz")
;=> ("foo" "" "bar" "" "baz" "")

(ppcre:split "(\\s+)" "foo bar   baz")
;=> ("foo" "bar" "baz")

(ppcre:regex-replace "(?i)fo+" "Foo bar" "frob" :preserve-case t)
;=> "Frob bar"
;   t

(ppcre:regex-replace-all "(?i)fo+" "foo Fooo FOOOO bar" "frob" :preserve-case t)
;=> "frob Frob FROB bar"
;   t

Yahoo Japan! で検索して結果を表示するサンプル

(defparameter *yahoo-search-result-scanner*
  (ppcre:create-scanner
   "
<div\\ class=\"hd\">
  .*?
  <a .*? href=\"(.*?)\" .*? >   # $1 = link
    (.*?)                       # $2 = title
  </a>
  .*?
  <div\\ class=\"bd\">
    <p>
      (.*?)                     # $3 = snippet
    </p>
"
   :case-insensitive-mode t ; タグの大文字小文字の違いは無視
   :single-line-mode t      ; ドットが改行にもマッチするようにする
   :extended-mode t         ; 拡張正規表現を有効にする
   ))

(defun cleanup-text (html)
  (string-trim " \n" (substitute-string html "<[^<>]+>" "")))

(defun scan-yahoo-search-result (res)
  (let (result)
    (ppcre:do-register-groups (url
                               (#'cleanup-text title)
                               (#'cleanup-text snippet))
        (*yahoo-search-result-scanner*
         (xhr:xhr-response-text res)
         (nreverse result))
      (push (list url title snippet) result))))

(defun yahoo-search (query)
  (xhr:xhr-get "http://search.yahoo.co.jp/search"
               :query `(:ei "UTF-8" :p ,query)
               :encoding *encoding-utf8n*
               :since :epoch
               :key #'scan-yahoo-search-result
               ))

(yahoo-search "cl-ppcre")
;=> (("http://weitz.de/cl-ppcre/"
;     "CL-PPCRE - Portable Perl-compatible regular expressions for ..."
;     "28 Oct 2009 ... A portable Perl-compatible regular expressions for Common Lisp. [Open source,   BSD-style license]")
;    ("http://faroffsea.blogspot.com/2009/01/lisprubypython6-common-lisp.html"
;     "遠い海: lispとrubyとpythonと その6 正規表現(common lisp)"
;     "2009年1月1日 ... lispとrubyとpythonと その6 正規表現(common lisp). Common Lispでの正規表現。   cl-ppcreを使う。cl-interpolを入れとくと正規表現リテラルが使えるので、これも入れと  いた方がよい。 ;;正規表現はcl-ppcreで。 ;;asdfでインストールしておく ...")
;    ...)

DESCRIPTION

xl-ppcre は CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp を xyzzy に移植したものです。

xl-ppcre は以下の機能を提供しています。

  • Perl 互換の正規表現が使えます (一部制限があります)

  • シングルラインモード、マルチラインモード、拡張正規表現

  • cl-ppcre 互換の API

xl-ppcre は以下の機能は 提供していません

  • 肯定先読み、否定先読み、埋め込みコードなどは処理できません

    • xl-ppcre は Perl 互換の正規表現を xyzzy 用に変換するのみで、正規表現エンジンは xyzzy 組み込みのものを利用しているためです

  • 正規表現リテラル (リーダーマクロ)はありません

  • replace-string や replace-dialog で Perl 互換の正規表現の利用はできません

  • cl-ppcre の parse tree 形式での指定はできません

  • ユニコードプロパティは利用できません

xl-ppcre でサポートしている正規表現のメタ文字は以下のとおりです。

\                     メタキャラクタのエスケープ
^                     行頭にマッチ
$                     行末にマッチ
.                     改行文字を除く任意の 1 文字にマッチ
[...]                 文字クラスのどれか 1 文字にマッチ
[^...]                文字クラスの補集合のどれか 1 文字にマッチ

*                     直前の正規表現の 0 回以上の繰り返しにマッチ (={0,})
+                     直前の正規表現の 1 回以上の繰り返しにマッチ (={1,})
?                     直前の正規表現の 0 回か 1 回の繰り返しにマッチ (={0,1})
{M,N}                 直前の正規表現の M 回以上 N 回以下の繰り返しにマッチ
{M,}                  直前の正規表現の M 回以上の繰り返しにマッチ
{,N}                  直前の正規表現の N 回以下の繰り返しにマッチ
{N}                   直前の正規表現の N 回の繰り返しにマッチ

*?                    直前の正規表現の 0 回以上の最短の繰り返しにマッチ
+?                    直前の正規表現の 1 回以上の最短の繰り返しにマッチ
??                    直前の正規表現の 0 回か 1 回の最短の繰り返しにマッチ
{M,N}?                直前の正規表現の M 回以上 N 回以下の最短の繰り返しにマッチ
{M,}?                 直前の正規表現の M 回以上の最短の繰り返しにマッチ
{,N}?                 直前の正規表現の N 回以下の最短の繰り返しにマッチ

( )                   グルーピング
|                     選択
\1 ~ \9              後方参照
\&                    マッチした文字列全体
\`                    マッチした文字列に先行する部分
\'                    マッチした文字列に後続する部分

\f                    改頁
\n                    改行
\r                    キャリッジリターン
\t                    タブ
\xXX                  16進法で表す ASCII 文字

\d                    数字           [0-9]
\D                    数字以外       [^0-9]
\w                    英数字単語     [0-9a-zA-Z_]
\W                    英数字単語以外 [^0-9a-zA-Z_]
\s                    空白           [ \t\n\r\f]
\S                    空白以外       [^ \t\n\r\f]

\A                    文字列の先頭にマッチ
\z                    文字列の最後にマッチ
\b                    単語の境界にマッチ
\B                    単語の境界以外にマッチ

\l                    次のキャラクタを小文字に
\u                    次のキャラクタをTitlecaseに
\L                    \Eまでを小文字に
\U                    \Eまでを大文字に
\E                    \L \U を終了

(?:regexp)            部分正規表現のグルーピングを行うが,後方参照を行わない
(?imsx-imsx)          正規表現のオプションを指定
                      - Perl と違い文字列の先頭でのみ使用可能
                      - i, s, m, x はそれぞれ独立に使用可能
                      - ハイフンが先行している場合にはそのオプションを打ち消す
                      - create-scanner のキーワード引数よりも優先

                        i  大小文字の違いを無視する
                           :case-insensitive-mode 引数と同じ意味

                        s  シングルラインモード (. が改行にマッチ)
                           :single-line-mode 引数と同じ意味

                        m  マルチラインモード (^ と $ が改行の直前と直後にマッチ)
                           :multi-line-mode 引数と同じ意味

                        x  拡張正規表現を有効にする
                           :extended-mode 引数と同じ意味

\<                    単語の開始にマッチ
\>                    単語の終了にマッチ
\sc                   シンタックスが c の 1 文字にマッチ
\Sc                   シンタックスが c 以外の 1 文字にマッチ
\_<                   シンボルの開始位置にマッチ
\_>                   シンボルの終了位置にマッチ
\_b                   シンボルの境界にマッチ
\_B                   シンボルの境界以外にマッチ
\_s                   シンボル文字にマッチ
\_S                   シンボル文字以外にマッチ

INSTALL

  1. NetInstaller で xl-ppcre をインストールします。

  2. xl-ppcre はライブラリであるため自動的にロードはされません。必要な時点で require してください。

REFERENCE

パッケージ名は xl-ppcre (ニックネームは ppcre) です。

xl-ppcre パッケージからは以下の関数を export しています。API の仕様についてはリンク先の cl-ppcre のドキュメントを参照してください。

  1. Scanning

    1. create-scanner

    2. scan

    3. scan-to-strings

    4. register-groups-bind

    5. do-scans

    6. do-matches

    7. do-matches-as-strings

    8. do-register-groups

    9. all-matches

    10. all-matches-as-strings

  2. Splitting

    1. split

    2. regex-replace

    3. regex-replace-all

  3. Miscellaneous

    1. quote-meta-chars

  4. Conditions

    1. ppcre-error

    2. ppcre-invocation-error

    3. ppcre-syntax-error

    4. ppcre-syntax-error-string

    5. ppcre-syntax-error-pos

以下の変数は cl-ppcre との互換性のために定義してありますが機能しません。

  • *property-resolver*

  • *regex-char-code-limit*

  • *use-bmh-matchers*

  • *optimize-char-classes*

  • *allow-quoting*

  • *allow-named-registers*

以下の関数は提供していません。

  • parse-tree-synonym

  • define-parse-tree-synonym

  • parse-string

  • create-optimized-test-function

  • regex-apropos

  • regex-apropos-list

TODO

優先度高

  • (?msx-msx) は正規表現途中での部分的な適用を可能にする

  • (?i-i) と括弧数のカウントを convert-from-psre 内で処理する

  • ドキュメント

    • cl-ppcre と互換性がない部分をドキュメントに書く

      • 正規表現が “” の場合

    • xyzzy との違いを書く

      • multi-line-mode がデフォルトで off

    • create-scanner の戻り値は compiled-regexp なのでscan-buffer などの引数に渡せることを書く

優先度低

  • create-scanner

    • regex が文字列リテラルや defconstant なら、do-xxx のマクロ展開時に正規表現のコンパイルまでしてしまう

    • parse tree のサポート

  • ユニコードプロパティ

KNOWN BUGS

なし。

要望やバグはGitHub Issues@miyamuko まで。

AUTHOR

みやむこ かつゆき <miyamuko@gmail.com>

xl-ppcre は MIT/X ライセンスに従って本ソフトウェアを使用、再配布することができます。

Copyright (c) 2011 MIYAMUKO Katsuyuki.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

xl-ppcre では以下の表示のもと psre-reader.l を改版して利用しています。

Copyright (C) 2011 MIYAMUKO Katsuyuki.
Copyright (C) 2009-2010 Yousuke Ushiki
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The name of the contributors may not be used to endorse or promote
   products derived from this software without specific prior
   written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE!
POSSIBILITY OF SUCH DAMAGE.

About

Portable Perl-compatible regular expressions for xyzzy Lisp

Resources

License

Stars

Watchers

Forks

Packages

No packages published