Complete Guide For Regular Expression

 

Photo by Markus Spiske on Unsplash
R
egular expressions or regex puts a lot of people off, just because of its look at first glance. But once you master this it will open a whole new different level of doing string manipulation and the best part of it is that it can be used with mostly all of the programming language as well as with Linux commands. It can be used to find any kind of pattern that you can think of within the text and once you find the text you can do pretty much whatever you want to do with that text. By this example, you can get an idea of how powerful and useful regex is.

What is Regex?

If you are reading this post then most probably you already know what a regex is, if you don’t know here is a quick and easy definition

Regex stands for Regular Expression and is essentially an easy way to define a pattern of characters. The most common use of regex is in pattern identification, text mining, or input validation.

Let’s Get Regex...

Photo by Kevin Ku on Unsplash

As we have seen regex can be used to find a pattern in the given sentence lets just start this up by finding just character. We are going to have a look at regex with python as this is the programming language that I love to work with.

Python has a built-in package called re, which can be used to work with Regular Expressions.

Just to search simple word

Like every programming language, there are some special characters in regex and so we need to escape them in order to serve them. Let’s see what happens when we directly used them without using Escape Sequence

Now let’s see the output using Escape Sequence ( \ )

Let's have a look at all the Metacharacters

Photo by Marvin Meyer on Unsplash

Here we have seen all Regular Expressions now let’s see how we can combine all of them to get a wonderful result. Let’s see them with a real-world example.

Sample text

Now let’s remove all of the URL

Output:

Let's see how it worked. First of all, we used ^https that says starting with https and ? says it can either come one or it can’t come and then we used Escape Sequence to escape all our // and then the .* says any character can come any number of time and then we use \r\n to find all characters till the new line. And that how we were able to select all the URL and then we just python inbuilt re.sub to replace all URL with “ “ or just empty space.

Now let's remove all number

Output:

We want to remove all special character like @ &

Output:

Do we need to remember all the regular expressions?

Photo by Mathew Schwartz on Unsplash

The simple answer is no, you don’t have to remember all the regular expressions if you want to find any type of regular expression for most of the time you can google it and find it on StackOverflow or any similar website .So you might've thinking then why should we study this, to be simply put you the latest need to know what code are you coping from the internet as not always it may suits your need sometimes when you need to customize it the knowledge of regex will surely help.

I hope you like this post and you can also see my previous post to know why python is mostly used nowadays. Thanks for reading.

Post a Comment

1 Comments