Give Regex a Chance, Give Yourself a Chance

One of the most useful skills I have picked up during my year in industrial placement, is learning how to use regex. However useful it may be, I know that there are some who regularly express their disinterest in regular expression (Okay, not regularly, but I really wanted to put that joke in). Keep in mind, that this blog post is not for me to gloat on my /regexp?/ knowledge, but to encourage you to give regex a try. What’s the worst that could happen? Having more skills?

First, let’s briefly go over what Google has to say about regex.
“A sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text”

Thank you, Google. In 10 words or less, regex is used for pattern matching. In programming, pattern matching in a string is quite a powerful thing to have.

Just imagine you are developing a simple program, which takes in a text file and outputs all 10 character long words that start with a vowel. Without the use of regex, we would think of splitting the text file by empty spaces and newline characters, and then looping through each word to find the right match. With regex, we could just search for all instances with the following pattern [aeiou]\w{9}. “What?!” I know, right?

What’s more, many text editors and IDEs now have the support for regex when doing a text search in files or folders. One common use case for me was to find all numbers that are within a pair of brackets and replace them with something else, and all I had to search was /\(\d+\)/. Bam! Easy peasy.

I know they are fairly simple examples, but I hope they have shown you how great of a tool regex can be. I implore you to take an hour or two, get yourself a cup of your favourite drink, get comfortable and read up on a simple regex guide, like this one by MDN. You don’t have to go crazy in one go, and memorise the patterns for each Unicode character. Take baby steps, aim to understand the basic symbols like \d, \w, […], etc. I believe soon enough you will snowball your way through the other symbols!

Don’t let the crazy ones get to you. I am sure everyone has seen, at least once, a monstrous regex pattern, but don’t let them intimidate you. As an old Chinese saying goes:

A journey of a thousand miles begins with a single step
Everyone has to start from somewhere, so don’t be afraid of the symbols, don’t be afraid to construct the wrong pattern.

Give regex a chance, give yourself a chance!

Final point worth considering: When you get into it, regex might seem to be the “one to rule them all”, but there are also cases where you should not use it. Regex is a useful tool, but keep in mind to always use the right tool for the job.