Yes, you can master regular expressions!
I spend most of my time nowadays going to high-tech companies and training programmers in new languages and techniques. Actually, many of the things I teach them aren’t really new; rather, they’re new to the participants in my training. Python has been around for 25 years, but for my students, it’s new, and even a bit exciting.
I suggested the first solution that came to mind: Regular expressions.
Regular expressions are a lifesaver for anyone who works with text. We can use them to search for patterns in files, in network data, and in databases. We can use them to search and replace. To handle protocols that have changed ever so slightly from version to version. To handle human input, which is always messier than what we get from other computers.
Regular expressions are one of the most critical tools I have in my programming toolbox. I use them at least a few times each day, and sometimes even dozens of times in a given day.
So, why don’t all developers know and use regular expressions? Quite simply, because the learning curve is so steep. Regexps, as they’re also known, are terse and cryptic. Changing one character can have a profound impact on what text a regexp matches, as well as its performance. Knowing which character to insert where, and how to build up your regexps, is a skill that takes time to learn and hone.
Many developers say, “If I have a problem that involves regular expressions, I’ll just go to Stack Overflow, where my problem has likely been addressed already.” And in many cases, they’re right.
But by that logic, I shouldn’t learn any French before I go to France, because I can always use a phrasebook. Sure, I could work that way — but it’s far less efficient, and I’ll miss many opportunities that would come my way if I knew French.
Moreover, relying on Stack Overflow means that you never get a full picture of what you can really do with regular expressions. You get specific answers, but you don’t have a fully formed mental model of what they are and how they work.
But wait, it gets worse: If you’re under the gun, trying to get something done for your manager or a big client, you can’t spend time searching through Stack Overflow. You need to bring your best game to the table, demonstrating fluency in regular expressions. Without that fluency, you’ll take longer to solve the problem — and possibly, not manage to solve it at all.
Believe me, I understand — my first attempt at learning regular expressions was a complete failure. I read about them in the Emacs manual, and thought to myself, “What could this seemingly random collection of characters really do for me?” I ignored them for a few more years, until I started to program in Perl — a language that more or less expected you to use regexps.
So I spent some time learning regexp syntax. The more I used them, the more opportunities I found to use them. And the more I found that they made my life easier, better, and more convenient. I was able to solve problems that others couldn’t — or even if they could, they took much longer than I did. Suddenly, processing text was a breeze.
I was so excited by what I had learned that when I started to teach advanced programming courses, I added regexps to the syllabus. I figured that I could figure out a way to make regexps understandable in an hour or two.
But boy, was I wrong: If there’s something that’s hard for programmers to learn, it’s regular expressions. I’ve thus created a two-day course for people who want to learn regular expressions. I not only introduce the syntax, but I have them practice, practice, and practice some more. I give them situations and tasks, and their job is to come up with a regexp that will solve the problem I’ve given them. We discuss different solutions, and the way that different languages might go about solving the problem.
After lots of practice, my students not only know regexp syntax — they know when to use it, and how to use it. They’re more efficient and valuable employees. They become the person to whom people can turn with tricky text-processing problems. And when the boss is pressuring them for a
After you go through all 50 exercises, I’m sure that you’ll be a master of regular expressions. It’ll be tough going, but the point is to sweat a bit working on the exercises, so that you can worry a lot less when you’re at work. I call this “controlled frustration” — better to get frustrated working on exercises, than when the boss is demanding that you get something done right away.
If you have always shied away from learning regular expressions, or want to harness their power, Practice Makes Regexp is what you have been looking for. It’s not a tutorial, but it will help you to understand and internalize regexps, helping you to master a technology that frustrates many people.
To celebrate this launch, I’m offering a discount of 10%. Just use the “regexplaunch” offer code, and take 10% off of any of the packages — the book, the developer package (which includes the solutions in separate program files, as well as the 300+ slides from the two-day regexp course I give at Fortune 100 companies), or the consultant package (which includes the screencasts, as well as what’s in the developer package).
I’m very excited by this book. I think that it’ll really help a lot of people to understand and use regular expressions. And I hope that you’ll find it makes you a more valuable programmer, with an especially useful tool in your toolbox.