Home >Backend Development >PHP Tutorial >PHP—The difference between PCRE and POSIX regular expressions

PHP—The difference between PCRE and POSIX regular expressions

伊谢尔伦
伊谢尔伦Original
2016-11-21 17:06:291597browse

As of PHP 5.3.0, POSIX regular expression extensions are deprecated. There are some differences between POSIX regexes and PCRE regexes, and this article lists the most significant differences to know when switching to PCRE.

The PCRE function requires the pattern to be closed with a delimiter.

Unlike POSIX, the PCRE extension does not have dedicated functions for case-insensitive matching. Instead, support uses the i (PCRE_CASELESS) mode modifier to accomplish the same job. Other pattern modifiers can also be used to change the matching strategy.

POSIX functions look for the longest match starting from the left, but PCRE stops after the first legal match. It makes no difference if the strings don't match, but if they do, there will be a difference in results and speed. To illustrate this difference, consider the following example (from Jeffrey Friedl's book Mastering Regular Expressions). Using the pattern one(self)?(selfsufficient)? to match the string oneselfsufficient, PCRE will match oneself, but with POSIX, the result will be the entire string oneselfsufficient. Both substrings match the original string, but POSIX treats the longest as the result.

Function comparison table

POSIX

PCRE

ereg_replace() preg_replace()

ereg() preg_match()

eregi_replace() preg_replace()

eregi() preg_match()

split () preg_split()

spliti() preg_split()

sql_regcase() No equivalent function


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn