7

POSIX を PHP 用の PCRE に変換するユーティリティはありますか? 私は PCRE に関する PHP のマニュアルにやや混乱しています。PCRE に関する詳細情報を見つけようとしていますが、誰かがそのようなユーティリティを設計したかどうか疑問に思っていました。

または、次の変換方​​法を誰かが説明してくれれば、それも問題ありません。

ereg("^#[01-9A-F]{6}$", $sColor)

しかし、変換を教えてくれるだけでなく、それがどのように行われたかを説明してください.

4

3 に答える 3

6

preg_match("/^#[01-9A-F]{6}$/", $sColor)
In this case you only need to add the two delimiters.

In perl you can write something like

if ( s =~ /x.+y/ )
{ print "match"; }
As you can see the actual regular expression is encapsulated in //. If you want to set an option on the regular expression you put it after the second /, e.g. switching the expression to ungreedy by default /x.+y/U
pcre now emulates this behaviour. Though you have to call a function you also have to provide the delimiters and set the options after the second delimiter. In perl the delimiter has to be /, with pcre you can chose more freely
preg_match("/^#[01-9A-F]{6}$/", $sColor)
preg_match("!^#[01-9A-F]{6}$!", $sColor)
preg_match("#^\#[01-9A-F]{6}$#", $sColor) // need to escape the # within the expression here
preg_match("^#[01-9A-F]{6}$", $sColor)
pcre でも同じですが、表現内に現れない文字を選択するのが最善です。

于 2009-06-21T06:33:18.727 に答える
4

preg_match("/^#[01-9A-F]{6}$/D", $sColor)

D修飾子に注意してください。人々はいつもそれを忘れています。それがなければ$、最後の改行文字を許可します。「#000000\n」のような文字列はパスします。これは、POSIX と PCRE の微妙な違いです。

もちろん、[01-9]に書き換えることもできます[0-9]

于 2009-07-03T16:24:13.983 に答える
-1

ちなみに、PHP は PCRE と POSIX の両方の正規表現をサポートしています。POSIX 正規表現に関する PHP マニュアルのセクションは次のとおりです。したがって、それらを変換する必要はありません: http://www.php.net/manual/en/book.regex.php

于 2009-06-21T04:58:27.150 に答える