downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
<recodePattern Modifiers>
view the version of this page
Last updated: Thu, 21 Aug 2003

LXXXIX. Regular Expression Functions (Perl-Compatible)


The syntax for patterns used in these functions closely resembles Perl. The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.

The ending delimiter may be followed by various modifiers that affect the matching. See Pattern Modifiers.

PHP also supports regular expressions using a POSIX-extended syntax using the POSIX-extended regex functions..


Regular expression support is provided by the PCRE library package, which is open source software, written by Philip Hazel, and copyright by the University of Cambridge, England. It is available at


Beginning with PHP 4.2.0 these functions are enabled by default. You can disable the pcre functions with --without-pcre-regex. Use --with-pcre-regex=DIR to specify DIR where PCRE's include and library files are located, if not using bundled library. For older versions you have to configure and compile PHP with --with-pcre-regex[=DIR] in order to use these functions.

The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

Resource Types

This extension has no resource types defined.

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

Table 1. PREG constants

PREG_PATTERN_ORDER Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. This flag is only used with preg_match_all().
PREG_SET_ORDER Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. This flag is only used with preg_match_all().
PREG_OFFSET_CAPTURE See the description of PREG_SPLIT_OFFSET_CAPTURE. This flag is available since PHP 4.3.0 .
PREG_SPLIT_NO_EMPTY This flag tells preg_split() to only return only non-empty pieces.
PREG_SPLIT_DELIM_CAPTURE This flag tells preg_split() to capture parenthesized expression in the delimiter pattern as well. This flag is available since PHP 4.0.5 .
PREG_SPLIT_OFFSET_CAPTURE If this flag is set, for every occuring match the appendant string offset will also be returned. Note that this changes the return value in an array where very element is an array consisting of the matched string at offset 0 and it's string offset into subject at offset 1. This flag is available since PHP 4.3.0 and is only used for preg_split().


Example 1. Examples of valid patterns

  • /<\/\w+>/

  • |(\d{3})-\d+|Sm

  • /^(?i)php[34]/

  • {^\s+(\s+)?$}

Example 2. Examples of invalid patterns

  • /href='(.*)' - missing ending delimiter

  • /\w+\s*\w+/J - unknown modifier 'J'

  • 1-\d3-\d3-\d4| - missing starting delimiter

Table of Contents
Pattern Modifiers -- Describes possible modifiers in regex patterns
Pattern Syntax -- Describes PCRE regex syntax
preg_grep --  Return array entries that match the pattern
preg_match_all -- Perform a global regular expression match
preg_match -- Perform a regular expression match
preg_quote -- Quote regular expression characters
preg_replace_callback -- Perform a regular expression search and replace using a callback
preg_replace -- Perform a regular expression search and replace
preg_split -- Split string by a regular expression

add a note add a note User Contributed Notes
Regular Expression Functions (Perl-Compatible)
jpatrin at pnicorp dot com
24-Oct-2003 06:31
RegEx Coach is an extremely useful program which can show you graphically what parts of your regex match what test text, step through the matching process, give a textual representation of your regex and much more. I highly suggest it for debugging and creating regexes.
13-Oct-2003 12:29
An invaluable source for testing your regular expressions using the actual php functions, as well as getting the looping structure and syntax correct for preg_match_all() is here:

I ran across this page some time back, and the ability to parse external web pages for input certainly puts it in the winner's circle.. that and the author was nice enough to share the source with us :-)
superzouz at hotmail dot com
14-Jun-2002 11:47
This is the easiest tutorial for regular expressions I have found yet.
Beginners should start here.
This tutorial is originally written for Jscript, but I think everything applies to PHP.
(I had to chop the following url into small pieces to be able to post it here)

clay at killersoft dot com
12-Mar-2002 04:56
Here's a translation of Friedl's email address matching regexp:

Friedl's perl original is available here:
hrz at geodata dot soton dot ac dot uk
07-Mar-2002 03:33
If you're venturing into new regular expression territory with a lack of useful examples then it would pay to get familiar with this page:
gilh at mudbuginfo dot com
22-Aug-2001 08:09
There is some really good/simple documentation of Perl regular expressions at

<recodePattern Modifiers>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST