Alex Costantino

Information Technology DevelopmentI.T. Development

🇬🇷️ 🇮🇹 EN

Regular Expressions: the dark magic of parsing




Regular Expressions: the "dark magic" of parsing.
If only that were true... in reality, it's just a buggy piece of crap.
Okay, I'm exaggerating a bit, but let me tell you how I came to this conclusion.

The Beginning

As you already know, I'm the creator of StyleZero.
So, StyleZero has a parser.
This parser was originally built with custom matching, without any tricks like regular expressions.
At some point, I heard advice from a developer on Facebook who said regex saved him many hours of work.
That dev is very experienced, and I thought:

What the heck... I’ll give it a shot too!

The Implementation

Obviously, writing the entire regex mess for StyleZero in one go didn’t work, so I split it into variables.

The final result was a confusing mess, but it worked!
Long story short, I managed to do work that was previously spread across multiple files, in just one function.

It looked amazing!
Everything was working flawlessly, until... it wasn’t.
Haha

So What Happened?

A colleague who’s obsessed with StyleZero was complaining that when he added certain SVGs into the template, StyleZero just wouldn’t compile.
Nothing extreme, since he had found a workaround.
But aside from reproducing the same issue in other cases, I wanted to know what the problem was.

After doing some testing and removing parts of the template, I finally found out that StyleZero breaks when encountering an alphanumeric string between 13 and 16 characters long.

At first, I found it funny, because on one hand it could parse huge templates with no issue, and on the other, I could crash it with a simple file containing text like:
234n905ct395945

Anyway, after some research I realized the regular expressions were to blame.
I had to pull a 3–4 day streak to rewrite the parser from scratch, so that version 9.2.1 could be released.

All in all, it turned out for the best, because the new parser is significantly better than the old one both in structure and functionality.

So My Advice Is:

Okay, use regular expressions... but gently.
They're generally considered sloppy, so keep them for simple tasks only.


Written by: Human
Translated by: AI