Course Content

  • Regular Expression

Course Content

Python Regular Expressions

Hello guys. Welcome to LearnVern. We saw Encapsulation in the last topic.

In which we saw how to achieve Encapsulation by the use of Private variable & Protected variable.

Today we will see Regular Expression, in which we’ll make some searching patterns, some match patterns through which we will perform Regular Expression.

We will see both theoretical & practical in this as well.

To begin with, what is Regular Expression?

Regular Expression is called RegEx here in Python.

If you see the word RegEx somewhere, don’t get confused.

It means Regular Expression only.

Now Regular Expression is used for what?

It is used to define any particular search pattern.

Suppose you made a pattern & your string should match that pattern, it is 1 type of Regular Pattern.

It is used in a way of validation.

Whenever you are going to use Regular Expression in Python & use some particular method, you have to recall 1 thing that is you have to import the RE Module.

Import what? RE Module.

There are 2 types of Functions in Regular Expression.

1st is Match Function & 2nd is Search Function. Match Function & Search Function.

Let’s see in detail what these functions are about.

First of all, we will see about Match Function.

What is a Match Function?

The function matches the beginning of the string.

If the beginning is matching according to the given pattern, then your regular pattern will be true, it will be validated.

Okay? If it doesn’t, it will reply None to us.

Here, the defining syntax is re . (dot) match & you have to pass 2 arguments in it which is pattern & string.

Now what does pattern & string mean?

Pattern is through which your regular expression is to be matched.

Meaning you have to write a particular pattern through which your string will match.

And string is the thing that we have to match according to your regular expression.

Let’s start with the practicals of the Match function.

I have opened the Jupyter here.

I’ll markdown Match Function here.

I have already told you that in Match Function, if you want to use any regular expression’s method, then you’ll have to import it.

Import what? Import RE Module.

Let’s see how to do it.

I’ll write here, Import RE Module first.

Import RE.

First of all we have to import a pattern first which will match your string accordingly.

I took a variable named Pattern here in which we can give customised pattern…

Suppose I want to make a pattern, the pattern should start with R.

Why R? Because it is a regular expression pattern.

Then your string should start.

Then apply this sign that is the power sign, we call it the beginning sign in Regular Expression.

And the string should begin with ABC.

Now I’ll take a string here mystring & define a string ABCDEF.

Now I have to match this string according to the defined pattern.
I’ll take a variable X here where I’ll call the re . (dot) match function.

And I passed the pattern & mystring in it.

So whatever the answer is, will be stored in X.

As soon as we print X, we get the output that our re.match object has matched 3 characters in particular to our patterns.

0 to 3 patterns have been matched & which are those 3? They are ABC.

Hope you understood it this far.

What it did was that it matched your pattern with the string.

Suppose I erase off B from here & then run it, it showed me None.

Meaning, your Pattern did not match your string.

I’ll write B again. Run it.

And it will again show me the answer, Match.

Suppose I wrote D in the pattern.

It changed to 0 to 4 & in the match it changed to ABCD.

That it matches the given String.

So this is how the Match function works under regular expression.

Understood how the Match Function works?

Moving onto Search Function.

So what is a Search Function? Search is also a Regular Expression function in a way.

But it checks the whole string in one particular location.

Meaning if you have a particular string in a particular location & you want to check some pattern in it.

Suppose there is a string, in which I have given a certain city’s name...such as Ahmedabad.

I have given the name Ahmedabad in the pattern but I searched Surat in the string.

The pattern is not matching, right.

As it won’t search in that string, it won’t match a regular expression.

The syntax is the same here.

Re . (dot) search, you have to pass pattern & string in the argument.

The definition of pattern & string is the same as in Match Function.

There’s nothing new in this.

But we will now see the practicals about how this function works.

Going to Jupyter.

I’ll make a Tab here & markdown.

Search Function.

First of all, we have to input RE here.

Always remember that.

Import RE Module first. I’ll make a note here to make you recall.

Import RE Module.

I took a txt variable here. And I wrote “We are learning Python”.

So this is my text.

First & foremost, I’m making a pattern here.

You can directly write a pattern here, RE . (dot) search.

I have to check white space here to see at which position is the white space in my string.

What will I use for white space? I’ll use slash S.

And in which do I have to check? In txt.

The string we wrote is being used in txt.

I’ll write down “This expression is written for searching white space, blank space”.

Now we have to print at what location that white space is.

Print. Print what? X.

I’ll write here “The first white space”. Where is it?

I’ll print only X here.

It made me a whole object where it stated that the white space is from 2nd position to 3rd position & it matched it.

But if we want to check the exact position of whitespace.

If you use the . (dot) start method & it will tell you the starting position of whitespace is from 2nd. That means that after the E of We, the white space is starting.

Okay, I’ll take 1 more cell & in that, I’ll check if the word we’ve written in the pattern matches in a string or not. It gets searched or not.

Import RE.

I’ll write the txt here “The rain in Gujarat ''. I took one such string.

Now I have to make a regular expression of search.

I’ll write Re . (dot) search method.

Now what do I have to search for in that particular string?

Suppose I want to check Ahmedabad.

But we have not written Ahmedabad in that string.

Let’s see what answer it gives.

We have to check it in string.

Now we will print X directly. It gave us the answer: None.

Why? Because we have not written Ahmedabad anywhere in that string.

So this is how you can use String in Regular Expression.

Next we will see the Replace function.

If we see it in a general way, replace means to change something.

Here in Regular Expression as well, there is a Replace function.

So in the whole string, if you want to change that is replace any particular word, any particular pattern, or any particular character, you can use this Replace function.

The Replace function works in the same way as the Search & Match function in Regular function.

Now if you want to replace any particular thing, Regular Expression has an RE . (dot) sub method.

You can replace anything in the string by using Sub.

There is a specific syntax RE . (dot) sub.

First of all, you’ll have to define a pattern.

Pattern means whichever thing you want to replace.

You have to write whichever thing you want to replace.

2nd is the thing you want to replace it with!

Suppose I have a character here which I want to replace with another character.

The 2nd character will be passed as the 2nd parameter.

And 3rd is String. String means to pass the name of the variable in which the String is stored.

Now we will move onto the practicals to see how replace is used as well as how it works.

This is also an important function in Regular Expression which is used a lot in Python.

We shall see the practicals now to know how it works exactly.

I have marked down the Replace function.

First of all we have to Import.

Import what? RE module.

Why RE module? Because it is also a Regular Expression function.

I’ll add String here. String 1.

I’ll write that “LearnVern provides free online training”.

This is my string here.

Now I want to replace the string.

Suppose I want to write Python instead of Provide.

I want to make a sentence such as Learnvern Python free online training.

I’ll print “Before Replace”.

Suppose I print my string 1 here.

I’ll edit a bit to make it tidy for you.

So what is our Before Replace string?

Learnvern provides free online training.

Now I’ll add a comment After replacing provide with Python”.

I replaced provide with python.

Meaning in the sentence, Learnvern provides free online training, I want to print Python instead of provide.

I took a variable named Result here.

Here you have to use the sub function to replace.

RE . (dot) sub. So we used this function.

Now first of all we have to write a pattern.

The pattern we have to write is of Regular Expression so we wrote R.

And double quotes, as it is strings.

We have to change provide here and we have to write Python here instead.

And we have to do it in string 1. So we will pass string 1.

As soon as I print the result, that is After Replace.

Now after replacing it, as I print the result…

Did you see that it changed to Learnvern Python free online training.

Above it was provide. Below it changed to Python as we replaced it with Provide.

Did you understand how the Replace function works?

Okay, I’ll show you 1 more example.

I’ll import RE once again.

I’ll take a string here which is String 1 = Learnvern provides free online training.

Now I want to replace the small letters…

Suppose I’ll capitalise F of Free & P of Provide, O of Online & T of training.

So wherever there are small letters, there should be 0 printed.

0 in the place of small letters.

And it should be replaced by using the sub function of the RE Module.

How will we replace it?

First of all, we will print Before Replace String.

I’ll print String 1. This is our Before Replace.

And then...now what do you have to do? You have to replace it now.

You have to replace all the small letters with 0.

I’ll write a variable result here.

Took Re . (dot) sub module.

I wrote R here because of Regular Expression.

Now I have to make a Regular Expression.

A to Z. A dash Z.

Meaning I have to replace all the small letters with 0.

I’ll write 0 here. And pass string 1 here.

As soon as I print here result 1.

Meaning After Replace. Suppose I printed Result here.

See! Did you see that all the small letters got replaced by 0.

So this is how the replace function works.

I hope you understood how Replace works in Regular Expression.

Now, let’s see some differences between Match function & Search function.

Match function & Search function usually works in the same way but there are certain differences.

What are those?

Both return the first match of the substring found in the string but re . (dot) match searches only from the beginning of the string & return match object if found.

So what Match function actually does is, it starts from the first letter of the string only but it will be found as it keeps matching.

It will be found at the first letter of the string only.

But what Search will do is, it will search the whole string and then bring the result.

But Match will match each & every character from the beginning, it will print the result from when it starts matching until it actually matches.

While the Search function will search the whole string and bring out the particular thing that matches.

This is the main difference in both the functions.

That Match will start from the 1st character & Search will search the whole thing.

Understood?

In the next video, we will see Database where we will see Database installation & how it is used in Python.

See you in the next video everyone.

Thank you.

FAQs

Regular expressions in python is a pattern within text which can be used to pinpoint a particular type of data. The pattern is specified using a special syntax.

Regexes are shorthand for regular expressions, which are text pattern descriptions. A d in a regex, for example, stands for a digit character, which is any single integer from 0 to 9. To match a string of three numbers, a hyphen, three more numbers, another hyphen, and four numbers in Python, use the regex below.

Import the regex module with import re. Create a Regex object with the re. compile() function. ... Pass the string you want to search into the Regex object's search() method. ... Call the Match object's group() method to return a string of the actual matched text.

In search and replace procedures, regular expressions come in handy. Looking for a sub-string that matches a pattern and replacing it with something else is a common use case. You can reference capture groups from the search pattern in the replacement string in most regular expression APIs.

The "Basic" and "Extended" regular expressions are two types of regular expressions. The extended expression is used by a few tools such as awk and egrep. The "basic" regular expression is used by the majority of people.

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
App Store
397K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
13K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere