Perl-Compatible Regular Expressions
Perl has long been considered the
benchmark for powerful regular expressions. PHP uses a C library
called pcre to provide almost complete support for
Perl’s arsenal of regular expression features. Perl
regular expressions include the POSIX classes and anchors described
earlier. A POSIX-style character class in a Perl regular expression
works and understands non-English characters using the Unix locale
system. Perl regular expressions act on arbitrary binary data, so you
can safely match with patterns or strings that contain the NUL-byte
(\x00).
Delimiters
Perl-style
regular
expressions emulate the Perl syntax for patterns, which means that
each pattern must be enclosed in a pair of delimiters. Traditionally,
the slash (/) character is used; for example,
/
pattern
/.
However, any nonalphanumeric character other than the backslash
character (\) can be used to delimit a Perl-style
pattern. This is useful when matching strings containing slashes,
such as filenames. For example, the following are equivalent:
preg_match('/\/usr\/local\//', '/usr/local/bin/perl'); // returns true
preg_match('#/usr/local/#', '/usr/local/bin/perl'); // returns true
Parentheses (( )), curly braces ({}), square brackets
([]), and angle brackets
(<>) can be used as pattern delimiters:
preg_match('{/usr/local/}', '/usr/local/bin/perl'); // returns trueSection 4.10.8 discusses the single-character modifiers you can put after the closing delimiter to modify the behavior ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access