| May I ask what resources you'd recommend for learning more? |
I get this question this week, so to save fingers I thought I'd consolidate my answer on this thread.
Much of what was said above is still true, but resources change and tastes vary, so here is my take.
A. Online Resources
1. Starting out: Jan's tutorial
is a great place to start. Jan is the author of beautiful regex software (see section B) and a regex book (see section C). He knows his subject.
2. Learning more:
Once you know the very basics, in my view, this Regex Tutorial
is the best place to go for both basic and advanced syntax. I wrote it, so I am biased. Judge for yourself.
The page is huge and has a different approach. Instead of repeating the regex ABC you will find all over the blogosphere, this tutorial tries to cut diagonally, going to the questions you ask yourself when crafting expressions. For instance, there is a whole section just to disambiguate the many forms of (?
syntax. A section about whether to capture or to match. About the various contexts where regex will be useful. About conditionals. Etc.
There is also a second tutorial dedicated to a little-documented feature of PHP regex: Recursive Regex
3. Grand Master:
the PCRE manual. No, I don't mean the PHP manual's section on PCRE
, although that document is often helpful if you code in PHP. I mean the manual to the PCRE engine
, a manual written by Philip Hazel, the creator of PCRE. It goes into the engine to a level of detail that PHP regex fans will love. Others will find it over the top.
For me, there is only one tool: RegexBuddy. RB, as this tool is known by its fans, is an all-in-one expression crafter and debugger written by Jan Goyvaerts, the author of the tutorial mentioned in section A.1. Here is an automatically updated link to the latest RegexBuddy demo
RB "understands" many flavors of regex: PCRE, Java, Python, etc. At the simplest level, it's a three-box interface. In one box, you craft your expression. In the second box, you paste your test strings. In the third box, you automatically see the matches and captures. There is a "Use" function that adapts your regex to the functions of your programming language: VB.Net, PHP, Java etc. And many other powerful functions.
RegexBuddy is powerful, but you have to set it up right, and the power doesn't jump at you when you first install it, as the interface looks innocently simple. See my quick guide to RegexBuddy
for set-up tips and a tour of the interface.
O'Reilly has three regex books. I like two of them: Jan's cookbook, and Jeffrey's Mastering Regex
book. If you pick just one, I would recommend Jeffrey's, though it is short on certain topics. I have tried hard to plug the holes in both of these books in my regex tutorial---see section A.2 above on this thread.
The bottom line is that to learn something, you want to go at it from different directions, so I would highly recommend picking up at least one of these books.
I don't want to repeat myself, so for more information about these regex books (and others that I don't recommend), please see my review of Regex books
You know it, practice makes perfect. You can read and read, but until you put your fingers to the keyboard, your knowledge of regex will be secondhand. Two places to practice:
1. Your own code
. This goes without saying.
2. The DevShed regex forum
! Your code will probably not expose you to all the aspects of regex in a short period of time. But the posts on this forum will. And there are many experienced regex practitioners here from whom you can pick up things that may not jump at you right away---this happens to me, and I love it. We all learn from one another.
Okay, folks... I could go on, but if you use the resources mentioned on this post, you'll be busy for quite a while. By that point, any other resource I could mention at this stage will be (for 90% or more) a repetition of things you already know.
Wishing you lots of fun you your regex journey,