RegEx Introduction and Quick Reference

By Adam Piper (2004) - link. Last edited: 2005-07-07 (YYYY-MM-DD)
This quick reference guide can be distributed in whole or in part as you see fit, as long as Zend are given credit for the 'Basic Examples using Metacharacters' section.

Note: Most of this info is available directly from the Zend Website, there are a couple of original additions but most of it is ripped with comments.



A list of all the PCRE (Perl-Compatible Regular Expression) functions are available with full documentation in the PHP Manual, here's a quick link to the pertinent page.

Basic Examples using Metacharacters

Note that when I say 'bit' below, I really mean 'character or submatch', but don't worry about submatches just yet.

Extra note on the use of ? after metacharacters that specify matching a number of the prior bit, ie. ?, *, +, {}.

This behaviour matches the least possible amount of characters in order to complete the matching process, so while '/blah??'/ will match 'blah', it would really rather just match 'bla'. This comes into play heavily while submatching.

Special (Meta) Characters

Note

All regular expressions used with preg* must be surrounded by containers, these are usually slashes (/) but could just as well be hashes (or 'pound' signs as Americans would have it ;)). I generally use hashes, as when matching such strings as 'http://' I'd have to escape the slashes: '/http:\/\//'. Instead I use the regex '#http://#'.

Escaping also needs to be used when using any of the meta characters mentioned above, you can't just match '1 + 2' with '/1 + 2/', you have to escape the '+'. So use '1 \+ 2'. Remember that as '\' denotes an escape sequence, it too needs escaping '\\'.

Modifiers

Modifiers are used AFTER the regex eg. '/regex here/iU'. These are the most useful modifiers in my opinion, find more in the PHP Manual.

Submatching



Notes

  • See preg_match_all for more fun.
  • Regular expression quick ref sheet (PDF) available here
  • Perl Regex ref sheet (PDF) available here
  • Short reference sheet available here
  • The manual contains information on advanced topics not covered in this document; Assertions, Once-only subpatterns, Conditional subpatterns, Comments, Recursive patterns and Performances.