Thoughts, quotes and thoughts about regex
Tib
Posted on February 22, 2021
Regex
"Regex" is the small name of Regular expressions
Regex are one of the superpowers 💪 of Perl
It's a productivity superpower the same way knowing command line shortcuts and mastering vim.
Regex could become one of your programmer superpowers also if you decide to.
But "regex" have also a nefarious reputation because they are hard, error-prone and misused.
Damian Conway is a master of regular expressions but you can sometimes feel the pain in his mouth when he discusses regex
But on a more positive note, Damian said
It could be strange to advocate a wrong thing but sometimes you just don't need more than a one shot program, and for me it would be dumb (or irresponsible) to spend more time than needed just "for the sake of correctness".
I know it's not really the hype of these days where over engineering is better perceived than KISS principles (I exaggerate just a little) but it is pragmatism.
Regex are bad
Regex are bad, everybody knows, and here is the very famous SO answer "You can't parse [X]HTML with regex" about that 😀
Are they really?
Some people have played the "devil's advocate" and came with regex for XML like this great answer from Tom Christiansen where he used regex to tokenize then Perl to parse.
But even Tom Christiansen said
Seems also like PCRE are more than simple regular grammars but actually I can't say if mathematically Perl regex can or can't parse XML, regular expression page on wikipedia says that
Another interesting old link on this topic is REX. The abstract of it is
The syntax of XML is simple enough that it is possible to parse an XML document into a list of its markup and text items using a single regular expression. Such a shallow parse of an XML document can be very useful for the construction of a variety of lightweight XML processing tools
(I do not quoted this one on a picture since I'm too afraid to awake Cthulhu)
Then what are regex recommended for
While use of complex regex are subject to discussions, there are perfect and obvious use cases for regex:
- Validation of input
- Cleaning of input
- Restructuring of input
- Looking for substrings within input
Folklore
I discussed about WrongButGoodEnough regex that help you save the world on a Friday evening:
But writing regex is hard to master and you probably don't
One solution is to leave your cat typing the regex for you!
Perl folks are crazy about regex
I wanted to finish with this crazy fact, fasten your seat belts.
Perl folks are really serious about regex
This is mad.
Posted on February 22, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.