1 minute read

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.

Jamie Zawinski, in comp.lang.emacs

  • If you can use native string functions instead, use them. Regular expressions should be your last port of call.
  • 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.
    • For Python there’s Kodos.
    • jsregex.com
    • regexpal.com
    • 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.
  • Regex in Javascript?
  • In PHP PCRE regex is quicker than EREG, but you should always see if you can use quicker native functions such as strncasecmp, strpbrk and stripos instead.
  • 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. Also try Simple HTML DOM Parser.
  • Try getting help, if you do, tell us:
    • What goes in?
    • What do you want out?
    • Which language are you using?
    • Have you tried anything yet?

Hope this helps!

Updated:

Comments