php-doc-en/reference/pcre/pattern.posix.xml

130 lines
4.3 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<article xml:id="reference.pcre.pattern.posix" xmlns="http://docbook.org/ns/docbook">
<title>Differences from POSIX regex</title>
<para>
As of PHP 5.3.0, the <link linkend="book.regex">POSIX Regex</link> 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.
</para>
<para>
<orderedlist>
<listitem>
<simpara>
The PCRE functions require that the pattern is enclosed by <link
linkend="regexp.reference.delimiters">delimiters</link>.
</simpara>
</listitem>
<listitem>
<simpara>
Unlike POSIX, the PCRE extension does not have dedicated functions for
case-insensitive matching. Instead, this is supported using the
<emphasis>i</emphasis> (<literal>PCRE_CASELESS</literal>) <link
linkend="reference.pcre.pattern.modifiers">pattern modifier</link>. Other
pattern modifiers are also available for changing the matching strategy.
</simpara>
</listitem>
<listitem>
<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.
</simpara>
<simpara>
To illustrate this difference, consider the following example from
"Mastering Regular Expressions" by Jeffrey Friedl. Using the pattern
<literal>one(self)?(selfsufficient)?</literal> on the string
<literal>oneselfsufficient</literal> with PCRE will result in matching
<literal>oneself</literal>, but using POSIX the result will be the full
string <literal>oneselfsufficient</literal>. Both (sub)strings match the
original string, but POSIX requires that the longest be the result.
</simpara>
</listitem>
<listitem>
<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
<link linkend="regexp.reference.character-classes">character classes</link>
but POSIX collating elements, character classes and character equivalents
are not supported.
</simpara>
<simpara>
Supplying an expression with a character class that both starts and ends
with <literal>:</literal>, <literal>.</literal> or <literal>=</literal>
characters to PCRE is interpreted as an attempt to use one of these
unsupported features and causes a compilation error.
</simpara>
</listitem>
</orderedlist>
</para>
<para>
<table>
<title>Function replacements</title>
<tgroup cols="2">
<thead>
<row>
<entry>POSIX</entry>
<entry>PCRE</entry>
</row>
</thead>
<tbody>
<row>
<entry><function>ereg_replace</function></entry>
<entry><function>preg_replace</function></entry>
</row>
<row>
<entry><function>ereg</function></entry>
<entry><function>preg_match</function></entry>
</row>
<row>
<entry><function>eregi_replace</function></entry>
<entry><function>preg_replace</function></entry>
</row>
<row>
<entry><function>eregi</function></entry>
<entry><function>preg_match</function></entry>
</row>
<row>
<entry><function>split</function></entry>
<entry><function>preg_split</function></entry>
</row>
<row>
<entry><function>spliti</function></entry>
<entry><function>preg_split</function></entry>
</row>
<row>
<entry><function>sql_regcase</function></entry>
<entry><emphasis>No equivalent</emphasis></entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</article>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->