Regular Expressions Round-up
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.
Jamie Zawinski on August 12, 1997 - more information on the origin of this quote can be found on Jeffrey Friedl’s Blog.
Introduction
Regular expressions or 'regex' as they are commonly called are a very powerful tool. I've given some presentations on this subject in 2009/2010. This page is a short round-up of some advice and useful links for the attendees to refer back to.
Some things to keep in mind
- Regular expressions can be slow if your regex is complex and/or your subject string is long. Consider alternatives if possible, such as
explode(),
strstr(),
strpos(),
str_replace(),
the Filter extension (>= PHP 5.2.0)
and the ctype extension (bear in mind that the last one if locale dependent)
- If you decide to use regular expressions anyway, the PRCE implementation is preferred above the POSIX implementation.
- If your string has to be evaluated numerous times to determine whether there is a match or not, your regex will be ...slow... Be as specific as you can about what you want to match.
- Regular expressions are greedy by nature. You can change this behaviour by either using the /U switch or by using ungreedy multipliers such as +? (once or more, but as few times as possible)
- Whenever relevant, use negative selectors - non-greedy by nature. Example: [^\n\r]+ will match everything until the next line-break.
- Only remember what you need. As the (...) syntax is also used for grouping, it is very common to remember a lot more than you need. Use the (?:...) syntax to group without remembering.
- Switches can radically alter the result of your regex:
/..../i = case-insensitive
/..../u = unicode
/..../U = ungreedy
etc
If you attended my Regex-Fu talk at the PHP Benelux 2010 Conference, your feedback would be much appreciated !
More information
Below you'll find some lists of useful resources. The listings are in completely biased and arbitrary order, i.e. based on my personal preference and where I don't have one, in random order.
About regular expressions: Manuals and Tutorials
Example regular expressions
Regex Cheatsheets
Testing regular expressions
Security considerations
Other useful resources
Contact me:
For feedback, additions or raving enthousiastic thank-you mails, you can contact me by e-mail: