PHP Regex Quick Reference

Pattern Reference

        <tr><td>[abc]</td><td>A single character: a, b or c</td></tr>
        <tr><td>[^abc]</td><td>Any single character but a, b, or c</td></tr>
        <tr><td>[a-z]</td><td>Any single character in the range a-z</td></tr>
        <tr><td>[a-zA-Z]</td><td>Any single character in the range a-z or A-Z</td></tr>
        <tr><td>^</td><td>Start of line</td></tr>
        <tr><td>$</td><td>End of line</td></tr>
        <tr><td>\A</td><td>Start of string</td></tr>
        <tr><td>\z</td><td>End of string</td></tr>
        <tr><td>.</td><td>Any single character</td></tr>
        <tr><td>\s</td><td>Any whitespace character</td></tr>
        <tr><td>\S</td><td>Any non-whitespace character</td></tr>
        <tr><td>\d</td><td>Any digit</td></tr>
        <tr><td>\D</td><td>Any non-digit</td></tr>
        <tr><td>\w</td><td>Any word character (letter, number, underscore)</td></tr>
        <tr><td>\W</td><td>Any non-word character</td></tr>
        <tr><td>\b</td><td>Any word boundary character</td></tr>
        <tr><td>(...)</td><td>Capture everything enclosed</td></tr>
        <tr><td>(a|b)</td><td>a or b</td></tr>
        <tr><td>a?</td><td>Zero or one of a</td></tr>
        <tr><td>a*</td><td>Zero or more of a</td></tr>
        <tr><td>a+</td><td>One or more of a</td></tr>
        <tr><td>a{3}</td><td>Exactly 3 of a</td></tr>
        <tr><td>a{3,}</td><td>3 or more of a</td></tr>
        <tr><td>a{3,6}</td><td>Between 3 and 6 of a</td></tr>

Pattern Description

Additional Notes

The following should be escaped if you are trying to match that character
\ ^ . $ | ( ) [ ] * + ? { } ,

Special Character Definitions
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase until \E (think vi)
\U uppercase until \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters until \E
Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)


 PHP  Regular Expressions  Reference