<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Differences from POSIX regex</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="reference.pcre.pattern.differences.html">Differences From Perl</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.pcre.html">PCRE Functions</a></div> <div class="up"><a href="pcre.pattern.html">PCRE Patterns</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="reference.pcre.pattern.posix" class="article"> <h1>Differences from POSIX regex</h1> <p class="para"> As of PHP 5.3.0, the <a href="book.regex.html" class="link">POSIX Regex</a> extension is deprecated. There are a number of differences between POSIX regex and PCRE regex. This page lists the most notable ones that are necessary to know when converting to PCRE. </p> <p class="para"> <ol type="1"> <li class="listitem"> <span class="simpara"> The PCRE functions require that the pattern is enclosed by <a href="regexp.reference.delimiters.html" class="link">delimiters</a>. </span> </li> <li class="listitem"> <span class="simpara"> Unlike POSIX, the PCRE extension does not have dedicated functions for case-insensitive matching. Instead, this is supported using the <em class="emphasis">i</em> (<em>PCRE_CASELESS</em>) <a href="reference.pcre.pattern.modifiers.html" class="link">pattern modifier</a>. Other pattern modifiers are also available for changing the matching strategy. </span> </li> <li class="listitem"> <span class="simpara"> The POSIX functions find the longest of the leftmost match, but PCRE stops on the first valid match. If the string doesn't match at all it makes no difference, but if it matches it may have dramatic effects on both the resulting match and the matching speed. </span> <span class="simpara"> To illustrate this difference, consider the following example from "Mastering Regular Expressions" by Jeffrey Friedl. Using the pattern <em>one(self)?(selfsufficient)?</em> on the string <em>oneselfsufficient</em> with PCRE will result in matching <em>oneself</em>, but using POSIX the result will be the full string <em>oneselfsufficient</em>. Both (sub)strings match the original string, but POSIX requires that the longest be the result. </span> </li> <li class="listitem"> <span class="simpara"> The POSIX definition of a "character class" differs from that of PCRE. Simple bracket expressions to match a set of explicit characters are supported in the form of PCRE <a href="regexp.reference.character-classes.html" class="link">character classes</a> but POSIX collating elements, character classes and character equivalents are not supported. </span> <span class="simpara"> Supplying an expression with a character class that both starts and ends with <em>:</em>, <em>.</em> or <em>=</em> characters to PCRE is interpreted as an attempt to use one of these unsupported features and causes a compilation error. </span> </li> </ol> </p> <p class="para"> <table class="doctable table"> <caption><strong>Function replacements</strong></caption> <thead> <tr> <th>POSIX</th> <th>PCRE</th> </tr> </thead> <tbody class="tbody"> <tr> <td> <span class="function"><a href="function.ereg-replace.html" class="function">ereg_replace()</a></span></td> <td> <span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span></td> </tr> <tr> <td> <span class="function"><a href="function.ereg.html" class="function">ereg()</a></span></td> <td> <span class="function"><a href="function.preg-match.html" class="function">preg_match()</a></span></td> </tr> <tr> <td> <span class="function"><a href="function.eregi-replace.html" class="function">eregi_replace()</a></span></td> <td> <span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span></td> </tr> <tr> <td> <span class="function"><a href="function.eregi.html" class="function">eregi()</a></span></td> <td> <span class="function"><a href="function.preg-match.html" class="function">preg_match()</a></span></td> </tr> <tr> <td> <span class="function"><a href="function.split.html" class="function">split()</a></span></td> <td> <span class="function"><a href="function.preg-split.html" class="function">preg_split()</a></span></td> </tr> <tr> <td> <span class="function"><a href="function.spliti.html" class="function">spliti()</a></span></td> <td> <span class="function"><a href="function.preg-split.html" class="function">preg_split()</a></span></td> </tr> <tr> <td> <span class="function"><a href="function.sql-regcase.html" class="function">sql_regcase()</a></span></td> <td><em class="emphasis">No equivalent</em></td> </tr> </tbody> </table> </p> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="reference.pcre.pattern.differences.html">Differences From Perl</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.pcre.html">PCRE Functions</a></div> <div class="up"><a href="pcre.pattern.html">PCRE Patterns</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>