DEV Community

loading...
Cover image for Thoughts, quotes and thoughts about regex

Thoughts, quotes and thoughts about regex

thibaultduponchelle profile image Tib ・3 min read

Regex

"Regex" is the small name of Regular expressions

Regex are one of the superpowers 💪 of Perl
Perl <3 Regex

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.
True power

But "regex" have also a nefarious reputation because they are hard, error-prone and misused.
100

Damian Conway is a master of regular expressions but you can sometimes feel the pain in his mouth when he discusses regex
Pain

Or even
Misery

But on a more positive note, Damian said
WrongButGoodEnough

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".
David Goodenough

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 😀
Idiot

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
Should

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
Regular not regular

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:
Save

But writing regex is hard to master and you probably don't
Trial

One solution is to leave your cat typing the regex for you!
Cat regex

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
Serious

This is mad.

Discussion (1)

pic
Editor guide
Collapse
raigaurav profile image
Gaurav Rai

Working with Perl has made me fall in love with regex - they are built in. Then I see other languages and it has increased my love and respect for Perl :P. PCRE(Perl Compatible Regular Expressions) is industry standard and it helps me write regex anywhere.