Welcome back.
Up until now, we've found just simple patterns.
Simple text patterns in our string.
Nothing too crazy.
But what regular expressions become really useful is for advanced patterns.
You see, with regular expressions you can do some crazy patterns like let's say this and this looks
like complete gibberish, right?
But this means something in regular expression.
You can think of regular expression as an entire language in itself with different patterns that you
use to find, well, patterns inside of text.
Now, the whole world of regular expression is really, really complicated.
As a programmer, I don't expect you to learn all these patterns.
You can see some of them here.
We have special sequences that mean something like Slash DD returns a match where the string contains
digits number from 0 to 9.
You have things like sets over here where if you type in a regular expression this, it returns a match
for any characters alphabetically between A and Z, whether they're lowercase or uppercase.
So there's all these little shortcuts and tricks they can write in a regular expression.
And things can get really, really complicated.
So nobody actually memorizes these things.
You develop them as the need arises.
And as a matter of fact, later on in the videos, we'll do an exercise where we'll actually experiment
with this and create a password checker and an email checker.
Now, one of the tools that I really, really like is called RegEx 101.
It's super, super useful.
All you do is we'll pick Python and it will give you regular expressions and a testing ground for you
to test.
For example, if I wrote something like, Hey, how are you?
I can now enter regular expression here and try things up.
So, for example, if I type in Hey.
How are you?
Hmm.
Well, this doesn't really work.
Maybe I can add some quotes around here.
But no.
And it actually gives you some options and information as to what you can use.
So, for example, let's say we wanted to check if.
Well, right here, a character in the range A to Z or A to Z.
If I copy this.
And I paste it in.
Look that.
It gives me full matches that I have.
In this case, I am checking to see, hey, does does it match any character between A to Z?
Both lowercase and capitalized.
Everything is matching right now.
You see over here that we have match one, match two, match three, so on and so forth.
I can also group things so I can do brackets here.
And now this is the first capturing group.
And then I can do something else here.
Let's say in here I want let's say anything that is a.
So what does it mean here?
Well, I have an explanation on the right hand side.
The first capturing group says, hey, match a single character present in the list below.
So it tells you A to Z.
Capital A to Z.
And then the second capturing group is hey, match a single character present in the list below, which
is a.
Now in here, I have two capturing groups.
So the first capturing group and then the second capturing group.
If I add a dot here, which is a special meaning and regular expression, if I hover over you see that
it matches any character except for line terminators.
So what am I doing here?
Well, I am first have.
Hey, I want you to check for the first capturing group, which is any single character in the range
between A to Z.
And then after that, I want any character.
And then after that, I want you to search well for any matches of a.
So this is what we match for.
We might match W because we're looking for any letter and then a space or any character and then an
A.
So now we have a full match which is w2a and then we have group one.
Which is w because it matches the first group and then group two, which matches the A.
Now I know this is confusing.
Regular expressions is one of those things that it's really hard to figure out at first.
So let me show you how we can use it.
If I go to code generator here, you'll see that I can actually just, well, give you a sample code
that you can use in Python.
But what I like doing is actually copying this piece of text.
Like this.
And going back to our code editor and in the compile.
Paste it in.
Now you'll notice that there's an R here and R stands for a raw string, because in Python, something
like a slash backslash has special meaning.
Because remember, with back slashes, we can do special things like new line or tab.
An R says, Hey, this is a raw string.
Ignore everything that Python interpreter might interpret.
I want this to just be a pure string so you'll see this a lot with regular expressions.
Now, if I run this code.
You see that I have ACA here.
Again, it's because I am searching for a.
Letter followed by anything followed by a.
And if I have a group here, which I'm searching through, if I do group one.
I get s if I do group to.
I get a just like we did in our playground here.
Another useful thing is in here.
If you look at the quick reference, you can actually search for things.
So let's say I wanted to check if a test string has maybe starts with.
So I can type in start here and you can see here that oh it gives me a quick reference.
So if I do start off string, it shows me, hey, use this character and it'll help you do startup string.
So if I do, let's say in here this carrot and say, Hey, look at that, it matches it for me.
So it says, hey, assert position at the start of line.
It explains exactly here how it works and it tells me that I have a full match here.
Now, most of the time, the way you'll be using regular expressions isn't like this.
It would actually be quite hard if you're just figuring everything out yourself.
Like I said, nobody really memorizes these things.
You can see that we have quick references with common tokens that you can look through and understand.
But a more practical way of actually learning about regular expressions is to see a real life use case.
Now I'm going to leave some exercises for you that you can do if you want to learn about some of the
common patterns from my experiences.
This is not something that anybody is going to ask you about in an interview.
You don't really have to memorize regular expressions.
You just need to know what they do.
So try the exercises if you want.
Otherwise, I'll see you in the next video to show you exactly how you would use regular expressions
in a real life scenario.
I'll see you in that one.
Bye bye.