Regular expression syntax examples

In order to give you a quick reference to the different patterns and what they will match, here's a comprehensive table of all we've covered. Column one contains example expressions, and column two contains what that expression will match.


Will match...


the string "foo"


"foo" at the start of a line


"foo" at the end of a line


"foo" when it is alone on a line


"Foo" or "foo"


a, b, or c


d, e, f, g, h, etc - everything that is not a, b, or c (^ is "not" inside sets)


any uppercase letter


any lowercase letter


any letter


any letter of number


one or more uppercase letters


zero or more uppercase letters


zero or one uppercase letters


3 uppercase letters


a minimum of 3 uppercase letters


1-3 uppercase letters


any non-numeric character


any symbol (not a number or a letter)


F, Fo, Foo, Fooo, Foooo, etc


Fo, Foo, Fooo, Foooo, etc


F, Fo


any character except \n (new line)


a word boundary. E.g. te\b matches the "te" in "late", but not the "te" in "tell".


a non-word boundary. "te\B" matches the "te" in "tell" but not the "te" in "late".


new line character


any whitespace (new line, space, tab, etc)


any non-whitespace character


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: A regular expression assistant >>

Previous chapter: Regular expression replacements

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.