Guide to regular expressions
A “regular expression” (or regex) essentially is the use of a pattern for string manipulation.
Regular expressions are used in many scripting and programming languages as they are a very powerful tool, allowing you to match and replace strings based on the supplied pattern.
Regular expressions aren’t always that simple, however there are things you can do to make it as easy as possible.
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
- Don’t be lazy, TRY.
- Read about what regular expressions actually are on Wikipedia.
- Don’t reinvent the wheel, you may find the solution is already out there, google for it or try the Regular Expression Library.
- Read Mastering Regular Expressions by Jeffrey Friedl. This book is not only a story, but its also a good resource for examples and reference.
- Testing regular expressions?
- The Regex Coach is a tool that comes highly recommended for testing your patterns.
- Try the Regex Powertoy, it’s a fantastic AJAX based matching tool.
- RegExR - a FREE desktop application for Mac, Linux and Windows.
- RegexBuddy - Not free, but meant to be good.
- Your favourite scripting language. PHP, Perl, Javascript, TCL, MRC, VBS, almost any of them!
- Cheat Sheets make good references, there is one at RegExLib.com and ilovejackdaniels.com.
- In PHP use pcre vs ereg, as they are quicker, and based on perl’s regular expression standards.
- For performance reasons it is recommended that you should always try and use the native functions instead of regex, especially in PHP.
- Stop parsing HTML with regexes and read how to parse HTML
- When parsing XML in PHP try xml2array, which makes use of the PHP XML functions, for HTML you can try PHP’s DOM document or DOM XML in PHP4.
- Try, getting help from #regex on the EFnet IRC Network, if you do, tell us:
- What goes in?
- What do you want out?
- Which language are you using?
- Have you tried anything yet?
- Still need help? I accept donations in return for my assistance.
Hope this helps!