是否有将 POSIX 转换为 PHP 的 PCRE 的实用程序?我对 PCRE 的 PHP 手册有些困惑,虽然我会尝试查找有关 PCRE 的更多信息,但我想知道是否有人设计了这样的实用程序。
或者,如果有人能解释如何转换以下内容,那也可以:
ereg("^#[01-9A-F]{6}$", $sColor)
但请解释它是如何完成的,而不仅仅是告诉我转换。
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}$
", $s颜色)
与 pcre 一样,最好选择表达式中没有出现的字符。
preg_match("/^#[01-9A-F]{6}$/D", $sColor)
注意D
修饰符。人们总是忘记它。没有它,$
将允许最后一个换行符。像“#000000\n”这样的字符串会通过。这是 POSIX 和 PCRE 之间的细微差别。
而且,当然,[01-9]
可以重写为[0-9]
.
顺便说一句,PHP 支持 PCRE 和 POSIX 正则表达式。这是关于 POSIX 正则表达式的 PHP 手册部分,因此您不必转换它们:http ://www.php.net/manual/en/book.regex.php