Why don’t more programmers use regular expressions?


Why don't more programmers use regular expressions? by Jonathan Neve

Answer by Jonathan Neve:

I happen to enjoy regular expressions immensely. So immensely, that in my more rational moments, I feel the need to moderate my enthusiasm by warning people that they actually can't (or at least shouldn't) be used for everything.

The truth of the matter is anything that can be done without a regex probably should be. Of course, I don't mean that literally (since technically, there's nothing that can't be written without a regex), but some problems really lend themselves to regexes, whereas most don't.

In cases that can be solved simply without a regex, the plain solution will most of the time be:

  • Faster to write
  • Faster to debug
  • Faster to run

So again, much as I enjoy using regexes, and perhaps in fact because I enjoy using them so much, I need to hold myself back from using them everywhere. There are times when regexes absolutely shine, such as:

  • Scripts/throw-away/"write-only" code
    Write fast and don't worry about maintaining it
  • One-off search or search and replace
    If you need to grep the list of running processes to find and kill any process matching a certain pattern or started after a certain time, you can do it much faster and easier with a little one-liner involving a simple regex than you could any other way.
  • Very complex text manipulation
    Sometimes, runtime performance doesn't matter much, but using regexes can save a considerable amount of time by avoiding the need to write an all-out, full-blown parser.

    For example, I recently wrote a little Perl script to convert code written in Borland C++Builder to Delphi. This is made possible by the fact that both environments share the same standard libraries, so it's "just" a matter of converting the C++ to Object Pascal. In about 1000 lines of code, most of it regexes, I actually got it to work up to about 95% (100% would probably not be possible). It only took me a few months, no idea how I could have done that without regular expressions.

So in conclusion, I would say that regular expressions are a wonderful and highly addictive tool that should be added to the arsenal of every programmer, yet we must resist the urge to forget all the other unromantic, boring (and, sadly, often better) ways we have of doing simple things.

It should also be noted that the extended notation (/x, also known as the kill-joy option*) allows for turning cryptic regular expressions into readable chunks of code with whitespace and comments, and it might help break the elitism of the regex-writer-band if we were to use this option to make regexes that are intelligible to an actual human. Yeah, it spoils some of the fun, I know, but still… 🙂

In case you're thinking I'm exaggerating the quasi-cultishness/elitism of regex users, what do you think it shows that one of the most popular ressources online for help with regexes and Perl in general is called perlmonks.com? The Monastery Gates

*Not really, but that's how it can feel at times…

Why don't more programmers use regular expressions?


About AvatarNemo

V: Voilà! In view, a humble vaudevillian veteran cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valourous visitation of a bygone vexation stands vivified and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition! The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honour to meet you and you may call me V.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s