Welcome back.
Up until now, we talked about regular expressions quite loosely.
And I mean, you should understand that it helps us match different things together.
But the problem is, well, how can I really apply this to real life scenarios?
Well, a good use case is perhaps when you're collecting emails, maybe you're creating a startup.
This startup wants to collect, let's say, emails of interested customers.
Now we want to make sure that whoever is interested in our startup enters proper email addresses, right?
Because we don't want anybody to fill our databases or our Excel sheets with random.
Words.
We want to make sure that something is a correct email.
Otherwise it says, Hey, this is not a correct email.
Please try again.
And regular expression is a great thing to use for validation and just making sure that whatever a user
inputs or whatever another machine inputs into your code is in the correct form.
So let me show you an example of how I would do this.
Let's say I want to do an email validation.
Well, the first thing I would do is simply type in Python email validation, regex.
And here you see here that we have a lot how to check for valid email addresses.
And pretty much the first couple of answers are going to be what you're looking for.
I like this one email address, regular expression that works 99.99% of the time.
That's good enough for me.
So in here you see that we have some idea of what we can use if I scroll down.
Looks like we have regular expression for different languages.
All right, let's do Python.
I'm simply going to copy this.
And now in here, let's go to our regular expression, one on one and just copy this in here.
Now I get an error because you see here that the R is already included because I've selected Python
here.
So let's just remove.
The brackets.
Again, if we hover over here, it's going to tell us about each thing that it's trying to do.
And we also have the explanation.
So this is how I like using regular expression.
I find something online that people have used in the past.
I don't try to reinvent the wheel and then try to understand it.
So just to test this out, if I type in an email address here, look at that.
I get a match, I get a full match, and it matches the group one.
Let's go one by one.
First I see that I have the brackets.
So this is the capturing group.
I'm trying to capture one data point.
If let's say I enter another email here.
That's not going to capture it.
If I go like this, it's not going to capture it.
Because while we're looking for one capturing group.
Okay.
Next, I see that it's asserting a position at the start of the line.
So the start of the line, we want to make sure that it captures this.
So it matches a single character present in a set.
So this email can be anything, it could be letters, it could be numbers.
And then finally it could be underscore.
It could have a dot in there, it could have plus, it could have minus.
And then finally plus is a quantifier.
So it matches between one and unlimited times.
So this string can be as long as I want and it's still going to work.
It's going to accept plus minus.
It's going to accept underscore.
But it's not going to accept.
An exclamation mark because it's not in here.
All right.
So that's good.
What about next?
Well, the next part is the sign.
It's literally the sign.
We have to make sure we have it.
And then after the sign again, once again, we have the characters that we can use with a plus at the
end to say, Hey, you can have as many of these as you want.
Perfect.
Next we have a special one.
Matches the character dot literally so because dot and regex if I remove it actually means something
it matches any character one.
We want to make sure that the dot well is exactly what we're looking for.
There needs to be a dot for let's say dot com or k or dot net or dot org.
If instead I didn't have this slash, this could be any character.
This is still a matching string without a dot.
So again, special character.
Right here.
And again, it actually gives you explanations.
So if you scroll down through here, I'll tell you exactly what it does.
All right.
After the dot, once again, we have anything.
That we want within these square brackets then as long as we want.
And then finally this dollar sign says, hey, it has to end with this.
This is the end of the line.
Awesome.
So using just that, we're able to figure out exactly how to do an email validation.
So all I need to do now is, let's say go to code generator.
Copy this piece of code.
Again, we're using a raw string because we want to make sure that Python interpreter just doesn't try
to add any special abilities to it.
We just want to make sure that this is a raw string.
Make sure I end that.
All right.
Perfect.
So now inside of this string, let's say I have an email address that a user sends me, such as be at
B dot com.
If I just remove all of these and just do a pattern.
And let's just simply do well, let's just check if a exists if I click run.
All right, this matches.
But if somebody gives me a wrong email address or something that's not formatted properly, let's say
just like this.
And I click run.
I get none.
So I could do an if statement saying, hey, if there's no matching object, well then tell them, sorry,
try again.
Very, very cool.
So I have an exercise for you.
Using what you've just learned and definitely your Google skills, because with regular expressions,
you will Google.
You're not going to memorize any of this.
I mean, I don't.
What I want you to do is to create also a password checker.
So the password has to have a few requirements.
So the password has to be at least eight characters long.
And it can contain any sort of letters.
Numbers.
And you know what it can contain.
How about these symbols?
That's it.
So write a regular expression just like this, using this regex 101.
And in the next video, we're going to try and do this as well.
I'll see you in the next one.
Bye bye.