I know RegEx (just a bit)

26th May, 2017 - 2 min. read

If you consider the Regular Expression, RegEx to my friends, a language for aliens, well, nice to include you in my company!

Maybe you already know that it basically allows to find sequences of characters by means of pattern rules.

You might even know that since everything on our computer can be represented with characters, they can help tons in terms of data manipulation and transformation.

No matter your knowledge level on the topic, you’ve been always fascinated and curious about how it works and attracted by its potentials as well.

Learn the shit

A year ago I’ve taken action and started to learn the basics of regular expressions.
I’ve used RegExOne, a great resource to learn regex with simple and incremental step by step tutorials with interactive exercises to test out what you’ve learned.

I’ve followed all the lessons alongside all the exercises but, to be fair, I was far to be confident using it in real-world projects.

Despite that, I’ve started to use it from time to time going to avoid some spaghetti code to solve little problems in projects.

I’m still learning it. I’m not confident using it without some helper tool like Regexr, which, by the way, I strongly suggest to try it out.

Watching a reg-ex still makes me cringe but I can write simple expressions without much troubles.

Recently, I had to extract valid URLs out of an array of texts and the following snippet fit the task perfectly:

var url = mytext.match(/https?:\/\/\S*/g)

It returns an array with all the valid URL found in a given text source. You can see it in action here.

The logic behind that dark piece of string is surprisingly easy to learn and could be outlined as the follows:

It’s hard

Mastering regular expressions require dedication and don’t happen quickly.

I feel you. You are so scared by this just-a-little-bit opaque, but very efficient, tiny language.
And yes, you can learn it.

Spotted a typo or (likely) a grammar error? Send a pull request.