/ / WinChalow

正規表現パーザ[perl]

2004-12-20

正規表現の自動生成をするには、正規表現のパーザが必要になるなあ、ということでCPANを探してみたらあった。ラッキー。
YAPE: http://search.cpan.org/~pinyan/YAPE-Regex-3.01/
Regep: http://search.cpan.org/~pinyan/Regexp-Parser-0.10/

同じ人が作っているが、Regexp::Parser の方が新しいものの、YAPE の方が簡単である。YAPE::Regex::Explain というモジュールを使うと、与えた正規表現の解説をしてくれる。それなりに便利かもしれない。

The regular expression:

(?i-msx:reg(ular\s+)?exp?(ression)?)

matches as follows:

NODE         EXPLANATION
---------------------------------------------------------
(?i-msx:     group, but do not capture (case-insensitive)
     (with ^ and $ matching normally) (with . not
     matching \n) (matching whitespace and #
     normally):
---------------------------------------------------------
  reg          'reg'
---------------------------------------------------------
  (            group and capture to \1 (optional
       (matching the most amount possible)):
---------------------------------------------------------
    ular         'ular'
---------------------------------------------------------
    \s+          whitespace (\n, \r, \t, \f, and " ") (1
         or more times (matching the most amount
         possible))
---------------------------------------------------------
  )?           end of \1 (NOTE: because you're using a
       quantifier on this capture, only the LAST
       repetition of the captured pattern will be
       stored in \1)
---------------------------------------------------------
  ex           'ex'
---------------------------------------------------------
  p?           'p' (optional (matching the most amount
       possible))
---------------------------------------------------------
  (            group and capture to \2 (optional
       (matching the most amount possible)):
---------------------------------------------------------
    ression      'ression'
---------------------------------------------------------
  )?           end of \2 (NOTE: because you're using a
       quantifier on this capture, only the LAST
       repetition of the captured pattern will be
       stored in \2)
---------------------------------------------------------
)            end of grouping
---------------------------------------------------------
(2004-12-20 12:32:38)

permlink