[Previous] [Contents] [Next]


Regular Expression Syntax

A regular expression follows a strict syntax to describe patterns of characters. PHP has two sets of functions that use regular expressions: one set supports the Perl Compatible Regular Expression (PCRE) syntax, while the other supports the POSIX extended regular expression syntax. In this book we use the POSIX functions.

To demonstrate the syntax of regular expressions, we introduce the function ereg():

boolean ereg(string pattern, string subject [, array var])

ereg( ) returns true if the regular expression pattern is found in the subject string. We discuss how the ereg( ) function can extract values into the optional array variable var later in this section.

The following trivial example shows how ereg() is called to find the literal pattern "cat" in the subject string "raining cats and dogs":

// prints "Found a cat"
if (ereg("cat", "raining cats and dogs"))
  echo "Found 'cat'";

The regular expression "cat" matches the subject string, and the fragment prints "Found 'cat'".


[Previous] [Contents] [Next]