fetus Diary

2007/09/18(火) - Regex

じゃあ PHP は?

<?php
    $a = '0123abcd0123';
    if(preg_match('/(\p{IsAlpha}+)/', $a, $match)) {
        var_dump($match);
    } else {
        echo "not match\n";
    }
?>
[fetus@RIM ~]$ php hoge.php
PHP Warning: preg_match(): Compilation failed: unknown property name after \P or \p at offset 11 in /home/fetus/hoge.php on line 3
not match

……あっそう。

<?php
    $a = '0123abcd0123';
    if(preg_match('/([[:alpha:]]+)/', $a, $match)) {
        var_dump($match);
    } else {
        echo "not match\n";
    }
?>
[fetus@RIM ~]$ php hoge.php
array(2) {
  [0]=>
  string(4) "abcd"
  [1]=>
  string(4) "abcd"
}

…全然「Perl 互換」ちゃうやん

# 調べてみたら、\p と \P は全然違う用途に使う模様。PCRE さーん、Incompatible なんですけどー。 Perl 側でも POSIX の文字クラスは使えるからそっち側で書けば…か。

という記事を書くと、「環境書けやゴルァ」というのが目に見えているので書いておく。

[fetus@RIM ~]$ perl -v

This is perl, v5.8.8 built for x86_64-linux-thread-multi

Copyright 1987-2006, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

[fetus@RIM ~]$ php -v
PHP 5.2.2 (cli) (built: May 8 2007 08:19:20)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
[fetus@RIM ~]$ cat /etc/redhat-release
Fedora release 7 (Moonshine)
[fetus@RIM ~]$ uname -a
Linux RIM 2.6.22.5-76.fc7 #1 SMP Thu Aug 30 13:08:59 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux

コメント

コメントはありません。

名前
メール
コメント

※HTML タグは使えません。HTTP URL には自動リンクが張られます。

Captcha
画像から読み取れる文字を入力: