A regular expression to check for prime numbers
noulakaz.net
noulakaz.net
http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
Every now and then somebody rediscovers this, and in latter years it's made the social news sites regularly. And it's not even hosted on my own site! That's HTML somebody else made from a mailing list post, long before I ever blogged. I guess I peaked early.
Oh, and DUH: the point is not that one should use regular expressions for computation. It's to underscore that regexes are automata. And, it's kind of fun to unravel some of the obfuscation.
In general, the more powerful a tool is, the slower it is. Good coding requires understanding how much power you need and using the right tool -- not waving a sledgehammer around because you've discovered that it can solve all of your problems.
Edit: The expression in the articles seems to use some 'practical' extensions.
True. I was using the (incorrect) terminology of the article, wherein "regular expression" really means "irregular expression". Regular expressions are fine and can be matched quickly; throw in backreferences and you have a tool which is both very powerful (and can detect primes) and can be exceedingly slow.