Regex: A required skillset for developers and power users

Published:

Last updated:

Estimated reading time: 2 minutes

I care about information and I believe everything we do wraps around trying to find information, protecting it, and making use of it. There is Google, DuckDuckGo, and other search engines, there is grep for Unix-like system users, there are search panes on applications and websites.

Sometimes these media of information gathering may not be good/aggressive enough to find the exact outputs you expect especially when looking through files. This is why we learn how to do our searching and pattern matching like a boss.


Regex (Regular expression) is a way to specify your search with deeper intensity. It is somewhat like wildcards. But, this is beyond very generic wildcards we use in simple instances. You may refer to this as wildcards on steroids.

Paint an instance where you have to find every occurrence of gray and grey in a large pile of text. Running (gray|grey) through a regular expressions parser will find all the expected results which are “gray” and “grey”. The pattern used to find them simply means I want to find every occurrence of gray or grey.

Being aware of how the | (pipe) can be used as an OR operator, I can write a better pattern this way gr(a|e)y and it will find the same results as the earlier used pattern.

There are tokens to be passed into regulation expressions parser to get outputs and some of them are listed in the table below:

TokensCategoryMeaningExample Matches
\wMeta sequenceword charactersEmma, Wattson
\dMeta sequencedigits12345
\DMeta Sequencenon-digitsAndrew&@#c
\WMeta Sequencenon-word47585^#@
\sMeta Sequencewhite space
\SMeta Sequencenon white spaceABC123
a{3}QuantifierExactly three of aaaa
a+QuantifierOne or more of aaaaaaa OR a
a*Quantifier (Greedy)Zero or more of aaaaa
a?QuantifierZero or one of acat OR cup

My examples are probably not the best you will get to see and I can’t mention all the token types here as I will be turning this blog post into a regex dictionary. If you need to find more of them, I think the bottom right pane at regex101 is enough to feed you with all you want.

Summary:

Regular expressions are very necessary and I think every institution should consider it a as compulsory course for every computer science student and departments that do a lot of computing.
If you will like to see a graphical illustration of what happens while you use regular expressions, try out regexper. It produces neat graphical illustration of what roles the patterns play.

Regexr also does something similar to what regex101 provides. It’s your call to pick what’s best for you.

References:

regex

Newer post

Prioritizing critical CSS for speed

Older post

Good son, bad father: I'm leaving PHP but I love Laravel

Responses

You may respond to this post by referencing it on your blog, twitter, or any website