是否有一个实用程序将POSIX转换为PCRE for PHP?

时间:2009-06-21 04:47:38

标签: php regex posix utilities pcre

是否有一个实用程序可以将POSIX转换为PCRE for PHP?我对PCRE上的PHP手册感到有些困惑,虽然我会尝试在PCRE上找到更多信息,但我想知道是否有人设计过这样的实用程序。

或者,如果有人会解释如何转换以下内容,那也没关系:

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

但请解释它是如何完成的,而不仅仅是告诉我转换。

3 个答案:

答案 0 :(得分: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
", $sColor)
与pcre完全相同,最好选择一个未出现在表达式中的字符。

答案 1 :(得分:4)

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

请注意D modifier。人们总是忘记它。没有它$将允许最终的换行符。像“#000000 \ n”这样的字符串会通过。这是POSIX和PCRE之间的细微差别。

当然,[01-9]可以重写为[0-9]

答案 2 :(得分:-1)

顺便说一句,PHP支持PCRE和POSIX正则表达式。以下是关于POSIX正则表达式的PHP手册的部分,因此您不必转换它们:http://www.php.net/manual/en/book.regex.php