Thursday 15 May 2014

Regarding regex: two things I'd like everyone to do

G'day:
Today my mate / colleague Duncan Cumming is my muse. But - sorry Daddy Duncky - not in a good way.

There was a question on StackOverflow over night "Need regular expression to insert a numeric value between filename and extension?". Dunc's given a reasonable answer, but also perpetuated an old trope which seriously needs to be put to rest:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Thanks for that, Jamie Zawinski. And also thanks to Peter Boughton for giving Duncan a smack down on this. Peter is probably the chief CFML-community regular expression guru I know. Yes: more so than Ben Nadel.

The thing is... using regexes isn't that hard. Obviously one can do some incredibly complicated and impenetrable with them, but this is the same as with any technology. But I think stupid (if slightly amusing, granted) quotes like the above have gone a long way to making regular expressions seem mystical and complex to people. It's not. It's just pattern matching. They're no where near as complex as the CFML programming logic we work with every day.

So that's the first thing. Stop perpetuating this nonsense about them being tricky.

What's the second thing? Make a point of getting yourself up to speed with regex: it's a great tool to have in one's belt; especially in the text-processing-heavy environment we work in, in the web.

When I first started this blog I did a long series on how to use regular expressions, from a specifically CFML perspective. It starts with "Regular expressions in ColdFusion (part 1: overview)", and continues for another ten or so articles after that (and I just noticed I have never written the last one! Better get on with that). Obviously also Ben Nadel has also written a lot on how to use regular expressions too (google results for 'site:www.bennadel.com "regular expressions"').

If you've shied away from regex stuff because they're apparently difficult: please just have a look at the stuff above, which hopefully demonstrates they're not. And get up to speed with 'em. And if you have any questions... ask away.

Cheers.

--
Adam