Let's talk about regular expressions.
Now, regular expressions aren't unique to Python.
As a matter of fact, you see them all over programming languages.
But I'm going to teach you regular expression just quickly, because they are very useful, especially
when you're doing, let's say, validation, like checking that somebody has entered the right email
or you have password requirements or you want to check if a piece of string exists in a piece of text.
So this is something that we're going to explore in this video.
Regular expressions.
What are they?
Well, luckily for us, Python comes with a regular expression module.
Remember, regular expressions aren't Python specific.
You can use them pretty much in any language, but they're very useful for finding things in a piece
of text.
So let's say we have a string here and we can say string search inside of this.
Next, please.
There we go.
Now, let's say I wanted to search for something here.
First of all, could I do something like this?
Could I say search?
In string.
Let's print here.
And if I click Run.
I get true.
And this is a simple way for us to search something inside of a string.
So why do we even need regular expressions?
Well, regular expressions are a little bit more powerful than just this, because maybe we want to
search how many times search occurs in the string.
Or maybe we have a complicated string.
Let me show you what I mean.
The way we use something like regular expressions is using this format.
So I can say r e dot and then in here use search.
And as you can see here, we give it a pattern and a string and some flags.
We'll talk about that a little bit later.
So all we need to say is what pattern do you want to do?
Well, I want to search if this exists in the string so I can say this.
And then give it the actual string.
In our case.
Well, this is our string.
So if I run this.
Well, let's print out what happens here.
So let's do print.
If I click Run.
All right.
I get what I call a match object.
A regular expression gives us an output, not just true or false, but an actual object that tells us
a little bit more information about this.
And you can see over here, one of the information is the span.
So where it occurs in the string, it occurs at index of 17 and ends at index of 21.
So that's really useful.
Let's store this information and just say a here.
So what can we do with this match object?
Well, the first thing we can do is to check what we have.
So we have, first of all, span, which we've seen.
And if I run this.
It tells me where the string occurs as a tuple.
So index of 17 to index of 21.
I can also do start and start.
Well tells me when the text starts and I can also do group or actually I can also do end to show me
where it ends.
And then I can also do group and group returns the part of the string where there was the match and
let's say there was another this in here if I click run, you see that?
Well, it only gives me that piece of or part of the string that there was the match.
Now group is really useful when we're trying to do multiple searches.
So in our case, we are just doing one search.
So we have a pattern that we're trying to match, but there's ways that we can do multiple searches
within the one line.
But before I show you that, what happens if we do something that doesn't exist such as this with a
capital S?
If I click Run.
I get an error because now A is going to be none because if the regular expression doesn't find anything,
it's going to return none.
So it's either going to return a match object or none.
Now, how can we make this more advanced?
Well, one thing we can do is to create a pattern that we want to check for.
So let's say pattern here and this pattern, we're going to use regular expression dot compile.
And the compile is going to allow us to give, just like we did here, a pattern.
So let's say in here, I simply want to check for this.
Now, this pattern I can use instead of just doing our search.
I can just use the pattern now.
To do the search for me.
So, again, if I run this.
Well, now I don't have to pass it this.
I need to just pass it the string that we want to search.
See that this is being captured and I can use pattern now everywhere multiple times.
So let's say I can do pattern dot.
All the methods that we have, we have quite a lot, actually.
Let me show you some of the useful ones.
We have find all which finds all the.
Instances of the match.
So let's say in here be.
And Click Run.
Look at that.
I have this.
And this.
So I get both instances of this inside of a list.
I can also do something like full match and full match.
Well, making sure that everything matches fully.
So if I click run here, let's print, see here, click run.
I get none because I don't get a full match.
In order for a full match to return the match.
It has to be the exact string that we're searching.
So if I click Run.
There you go.
I get a match object now.
Now, let's say I have a question mark in here instead of an exclamation mark if I click Run.
It's none if I have exclamation mark and then maybe something else afterwards.
I still get none.
We also have another option which is match.
And match, as you can see, matches zero or more characters at the beginning of the string.
So if I do string here and I print dx well actually let me change this up.
Let's do here Andre in the string that we're actually comparing and if I click run.
I still get a match object versus if I do see which uses a full match.
Which gives me none because, well, we only want an exact match here versus a match that matches the
string and then doesn't care what comes afterwards.
So this is extremely useful.
But up until now, I mean, we get a couple of methods that we can use which are quite useful, but
this doesn't seem that that powerful.
Why do all languages have regular expressions?
Why do we really care?
Well, in the next video, we're going to explore this idea of pattern and how we can do some really
advanced patterns.
I'll see you in the next video.
Bye bye.