Motivating Regular Expressions

Mar 4, 2014 • Benjamin Bradshaw

Regular expressions are a difficult sell.  Not only does the typical “elevator pitch” description of them (“it’s like search and replace, but better!”) fail to capture how profoundly many day to day situations can benefit from regular expression knowledge, but the syntax is — to be very kind— unforgiving and baffling to novices.  It is undoubtedly on the level of Randomo.  I suppose my story then is a fairly typical situation for regular expressions.

I was working at a previous job editing a configuration file for some in-house application.  I was working with several outside consultants.  Though I can’t remember the specifics, I had to fix a botched configuration file — something like 25 lines of text had to have a keyword changed using some keyword earlier in the line — a pretty typical use case for a regex.  I had one of the consultants looking over my shoulder as I opened the file in an IDE with a built-in regular expression interpreter.  As I typed in the regex, he asked me what I was doing, so I explained the syntax of the command.  He said he was vaguely familiar with them but thought that they were too confusing to use unless they were the only way to solve a problem, i.e., in a script.  As if on cue, my first attempt at putting together the regex failed — my expression wasn’t matching the target text.  Grumpily he said he could have manually fixed the configuration twice in the time it would take me to figure out what was wrong.   I said, that may be true, but even if it would take 5 minutes to make the changes manually and 15 to correct the regular expression syntax, the new knowledge of what I was doing wrong (and how to do it correctly) would surely save far more than 10 minutes in the long run.  Now I wish I could tell you that I so eloquently explained this idea that he immediately agreed with me.  But unfortunately, he just shrugged and said if it were him, he wouldn’t bother “trying to be fancy.”  However, the next day the configuration file had to be changed yet again.  At that point, when I pulled up the regex in the history, and effortlessly completed the changes, he told me he was going to make another attempt at learning to regex.

Motivation-wise, my initial attempt to justify the additional work by describing it essentially as a long-term investment didn’t seem to be as effective as a simple real-world example of time-saving through repeated usage.  That being said, I think I would still use both appeals in a classroom.  It may have been the “combined effort” that eventually sold him, and of course, the context of a class may render the former approach more effective.