XML technologies / Regular expressions / PHP

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

PHP became popular in the late-1990s web boom and remains so. Its appeal probably lies in how quickly beginners can build something, while experienced developers also find a wealth of functions. PHP naturally supports regexes; indeed, it contains no fewer than three separate, independent regex engines.

PHP’s three engines are preg, ereg, and mb_ereg. This book covers only the preg family, whose NFA engine surpasses the other two in performance and features. Preg is usually pronounced P-Reg.

 

In this chapter

First, this chapter builds on the fundamentals from Introduction to regular expressions through The art of writing regular expressions. PHP readers may want to jump straight here, but I encourage them to benefit from the earlier material: the preface, especially notation, and chapters Introduction to regular expressions through Features and dialects for fundamentals and applications, then How regex engines work through The art of writing regular expressions for the operation of the regex engine, directly relevant to PHP’s preg engine. Key concepts include NFA matching, greediness, backtracking, and efficiency considerations.

This book is not intended as a reference, even if tables such as Overview of regex elements in PHP’s preg package, or the earlier Constructs covered in this section and Basic Unicode subproperties, suggest otherwise. My goal is to teach you to think in regexes.

The chapter starts with preg’s history and dialect, then details individual functions and specific efficiency issues, ending with extended examples.

 

Background and history

The name preg is a prefix for every function in the package and means “Perl Regular Expressions.” Andrei Zmievski added this family, dissatisfied with ereg. Ereg means Extended Regular Expressions, a POSIX implementation extended only compared with the simplest alternatives, and limited by present standards.

Andrei essentially wrote PHP bindings to PCRE, Perl Compatible Regular Expressions. PCRE is an excellent C regex library using an NFA and closely following Perl’s syntax and behavior.

Before choosing PCRE, Andrei examined Perl’s source for reusable code. Like many others, he found that impractical: although easy for users, Perl’s engine has accumulated contributions from so many developers that one person can barely understand it all.

Earlier, Philip Hazel at Cambridge University in England had a similar experience and wrote PCRE for his own purposes (see Bringing order to regex dialects). Unlike Perl’s developers, he had precise specifications and could start with clean structure. Years later, Andrei found well-written, documented, powerful code to integrate into PHP.

PCRE evolves alongside Perl and has different versions. This book covers PHP 4.4.3 and 5.1.4, both using PCRE 6.6. While researching earlier releases, I found bugs fixed in those versions; some examples may not work with older versions.

In PHP’s version scheme, the 4.x and 5.x branches are developed in parallel; 5.x is a new implementation. Independent development means a 5.x version can contain an older PCRE library than a 4.x version.

  

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top