Team LiB
Previous Section Next Section

A Few Final Words

When you understand how they work, regular expressions become the best thing since the invention of the wheel. However, you will find that getting to master regex is a long and difficult process, and it takes a while before the actual reasoning behind how they work starts sinking into your brain.

Generally speaking, the most difficult aspect of regular expressions is debugging them, because PHP doesn't really provide you with any facility to do so, and the language doesn't lend itself well to simple bug-finding techniques (like printing out a result at various stages of the execution). As a result, the best way to debug a regular expression is to get it right the first time. The approach that I recommend is to start small with a simple "core" of your regex and make sure that works without any problem. You can then add to it, one step at a time and checking your work every time, until you've reached the intended result. This way, it's more difficult to let the situation get out of control and lose track of what your expression does.

Another important thing to understand about regex is that they are not a panacea. Regular expressions are slower than straight string substitution functions and should therefore be used only when the latter are unable to provide a viable alternative. Finally, Perl regular expressions are often much faster than their POSIX counterparts. As a result, even though they are a bit more complicated and may take a while longer to master, you should consider making the effort and using the former as often as possible.

    Team LiB
    Previous Section Next Section