Is there a utility that will convert POSIX to PCRE for PHP? I'm somewhat confused by the PHP manual on PCRE, and while I'll try to find more info on PCRE, I was wondering if anyone had designed such a utility.
Or, if anyone would explain how to convert the following, that would also be fine:
ereg("^#[01-9A-F]{6}$", $sColor)
But please explain how it's done, not just tell me the conversion.
POSIX Basic Regular Expressions. POSIX or “Portable Operating System Interface for uniX” is a collection of standards that define some of the functionality that a (UNIX) operating system should support. One of these standards defines two flavors of regular expressions.
PCRE tries to match Perl syntax and semantics as closely as it can. PCRE also supports some alternative regular expression syntax (which does not conflict with the Perl syntax) in order to provide some compatibility with regular expressions in Python, . NET, and Oniguruma.
P stands for Perl in 'preg'. They comes under Function list.PCRE : Perl Compatible Regular Expressions.
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
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)all the same to pcre, best to chose a character that doesn't appear within the expression.
preg_match("/^#[01-9A-F]{6}$/D", $sColor)
Note the D
modifier. People forget about it all the time. Without it $
will allow a final newline character. A string like "#000000\n" would pass. This is a subtle difference between POSIX and PCRE.
And, of course, [01-9]
can be rewritten to [0-9]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With