Friday, February 13, 2015

Geek: regular expression insanity...

Geek: regular expression insanity...  Those readers who know me as a programmer know that I have an unnatural affinity for regular expressions.  I love the darned things!  I have happily spent several days carefully crafting a regular expression that can be written on a single line of text, to do something clever and challenging in the way of analyzing text.  But even I think this is over the line: a regular expression to test whether an integer is prime:
I don't know who to credit (?) for this monstrosity, but it's been floating around for quite a while. I first ran into it about 15 years ago. To test an integer for primality, you first convert the integer to a string of 1s (e.g., 5 becomes 11111, 7 becomes 1111111, etc.).  Then you see if the regular expression matches.  If it does, the number is not prime.  If the match fails, the number is prime.  I read a decent explanation of how it works this morning.

So why do I call this a monstrosity?  Well, it's a bit like a carpenter using repeated hammer blows to cut a piece off a 2x4, instead of using a saw.  It's the wrong tool.  The hammer is very useful – for driving nails or punishing regular expression abusers – but it's not good for cutting a piece of wood...

No comments:

Post a Comment