Regex Introduction | Learn Regular Expressions 01

Video Tutorial

Getting Started

Regex (Regular Expression) is a pattern describing a certain amount of text, used for Text parsing and data validation, available on all programming Languages since it is very important for any functionality that requires working with large data set.

Also used for validating complicated text, number or special chars patterns (for ex: validating email addresses, phone numbers…), there are modules available for regex either for js, python or any other programming language either builtin or as a library.

if you have never used this before, you will enjoy working with regular expressions across your apps from now on.

Installing Regex

Well, Javascript already has regex to work with it right of the box as well as many other, but you still can use 3rd party libraries or modules for extending the base functionality.

Try to search for Regex for the specific programming languages you want to use with, you will get the full guide on how to start working.

We are going to use Regexr, it is a web platform for running, testing and working with regular expressions alongside that it also has guides and references into Regex for guiding you into the right way to Master Regex, we are going to use it for creating a regular expressions for matching very basic patterns (Emails, Phone Numbers, and Ip Addresses)

Let’s Take a Look

Here’s how our Working Environment Would looks like, we are going to try to create the right Expressions for matching the text below (Phone Numbers, Emails, and IP addresses)

you can see the Regex Special Chars references from the left side on the Cheat Sheet

You can Use this as a reference as you try to solve a pattern puzzle.

Phone Numbers

Let’s Start by creating the right Expression for matching the phone Number either with a + (Country Code) at the beginning or without it.

So for matching a digit, you use \d (called: Escaped D) we use the backslash for letting it know that this is not a D letter, we put this between the two forward slashes and gm at the end which stands for (Match Global and Multiline)

It would look something like: / \d /gm but this would only match one letter, for making it match all we append the + (Take a look at the Cheat Picture)

For matching the + (Plus) at the beginning of the number and since it is optional which means we may find it or no, we use +? + for the plus char since it is a special char we escape it first and we use the question mark for telling it that the preceding char is optional, and that how you can match a phone number in a very easy and simple way, final Regex: ( / +?\d+ /gm ).

Email Addresses

We can do the same thing for email addresses but this time we are dealing with either digit, words and special chars (@ . )

First, we tell it to match words we use: \w+ for matching all characters and as you know an email address composed of two major parts, username and email server separated by the @ character and the ends with the domain extension (.com, org…) so here how it would look like: / \w+@\w+.\w+ /gm we match the first part (before the @) then second part and the extension at the end, notice here we use . to escape dot (.) character.

as easy as that you can match any email address so this will help you in the validation process.

IP Addresses

for IP addresses it’s a bit complicated than the other simple examples of matching phone numbers and emails, if you are familiar with email addresses architecture it is composed of 4 parts, each part can hold up to 3 numbers for ex: this is an IP address so we need to match the 4 parts separately we separate between each of them using a dot (.).

So we using the \d for matching a digit and [0–9] to tell it to match numbers between 0 to 9 which means all possible values separated by dots and we need to repeat this 4 times for matching all the parts.

For matching an IP Address: / ^[0–9]+.[0–9]+.[0–9]+.[0–9]+$ /gm

Pretty Nice.

What’s Next

Now you have got an Overview of what Regular Expressions are and what is used for, from now you can start diving into more complicated tasks such as parsing large data sets and dealing with complex patterns.

All we have covered so far is the basic thing on what can you do with Regular Expressions.

No Comments Yet