Welcome back.
In this video we will apply our knowledge of regular expressions to practical examples.
We'll explore real world scenarios and how to solve them using regex.
Let's get started.
All right.
How do I now figure out a specific pattern.
Because so far we have used very generic regular expressions.
We just used the very simple characters and so on and so on.
So what if I want to have this specific structure, let's say three digits, another three digits and
then four digits, and they are separated by the dash.
So how do I do it.
Well I say it's one digit, then it's another digit and it's another digit.
Then as you can see that fits for all those patterns that we have or all those numbers that we have
here.
That's because all of them are three numbers at least.
So if I add another one here, as you can see, the first line is highlighted.
Then here in our third line, only the ones with at least four numbers connected to each other is highlighted.
So what if I want to have this line three highlighted and line four as well?
Because they are with the same pattern, then I can do the following or the following thing like that
minus backslash d, backslash d, backslash d and backslash d.
So now I have those two lines or those two numbers highlighted.
This is a way how I can figure out all telephone numbers with this structure.
And there are easier ways to do that.
I don't have to enter so many DS if I don't want to.
There is a simpler way I can use the following approach.
So I can just say okay, within curly brackets I say how or I tell how many of the foregoing escaped
characters or patterns do I want.
So in this case I say okay, I want to have three DS, which means three digits.
The same thing goes for our second entry or second part of information.
And for the last one, as you can see, three is not enough.
We need four.
So now it fits those two patterns.
So what if I don't want only to have the minuses but also the dots or also the hashes?
Well, I can either use dot in both cases, but then as you can see everything is included.
So as you can see here we have three numbers.
Then we have something else.
And then we have a lot more numbers.
So we have actually this piece here.
That's why the line seven is marked as well.
Same for eight.
As you can see it starts after the 49 which means we have this pattern here as well.
But I don't want to have those patterns I want to be more precise.
I want to have, for example, the hashtag in there.
I want to have the minus and the dot, but I only want to have this specific structure.
So how do I get that?
Well, I can use a literal group search, which means instead of the dot I'm going to use square brackets
again and they will be my literal group search.
So in here I say either it's a dot or it's a hash or it's a minus.
So as you can see now, all of those three characters are fine, but nothing else.
Now, if I want to use it for the first character here as well.
So I had a dot here.
So it took the minus the dot and the hash.
But I want to have all three of them and not anything else.
So what if I enter an A here for example?
Well, now it's not the case anymore.
But if it's just a dot, then the A is fine.
But if I enter my group that I had there my specific literal group search.
So this is a literal group search here.
Now it's not included anymore.
So it has to be one of those three characters.
All right.
If you feel like a challenge and you're up for a real challenge, then create the structure for all
those three numbers.
So create a pattern for all those three different numbers, because these are three different ways of
creating a mobile phone number in Germany.
And that's the general structure.
So this is if you call from Germany, this is if you call from outside of Germany and that one as well.
So either a plus or double zero.
That's generally if you call someone outside of your country, you need to have the plus plus the country
code.
And it's the same with zero zero which represents this plus.
All right.
So if you want to you can create the structure for this one.
It's pretty difficult.
And I still need to show you one thing before you can do that.
And maybe it's a little more than just one thing.
So first of all let's go ahead with the pipe.
So let's say I want to have plus 49 or I want to have something like zero zero 49.
Now that by itself won't work.
Why is that?
Well, if we look at the snippets we can find quantifiers and we have not used quantifiers yet.
And that's something that we will need to do.
Now the same thing for grouping constructs and alternation constructs.
So these are the ones that we are going to use now okay.
We need to check those out before we can go ahead and create a whole structure for those numbers.
So let's start off with using alternation constructs and quantifiers.
So the plus says matches the previous element one or more times.
Well I don't want to do that.
I just want to have a literal plus.
And as you know a plus is a meta character and it needs to be escaped.
So what I need to do before the plus is a backslash.
And now you can see I'm using the pipe here.
So I say either it's plus 49 or it's zero zero 49.
And as you can see both those structures or both those patterns are marked here or both.
My numbers here are highlighted.
So that's what this pipe does.
It's either or.
So one of those has to be true in the whole number that we're looking at.
Then it's fine.
And that's not just for numbers.
That's for characters as well.
So for example if I want to have Mr. and Mrs..
So if I want to tag those I use Mr. Pipe miss which Which means either Mr. or Miss have to be true.
And it's the case here in our case.
So that's the either or.
As you can see here our alternation construct.
Then the grouping construct is something that we need to look at.
And in order to look at those I think it's best if we look at the links here.
So we have certain links here https and http links and either with or without.
So we have four different links here.
And if I want to use a group let's say I want to use this group w w w.
But as you can see it's not the case for all links.
So in order to find the links we need to first of all go to https.
Then we have this structure.
So far everything is good.
And now if I want to have those two still active I need to check out this group w w w because here it's
the case and here it's not.
So how do I do that.
Well I need to use a group.
So I'm going to group w w w.
And as you can see so far only this link here the first one is highlighted.
But then if I enter this question mark then it's fine.
So I'm using the group WW, but I'm combining it with the question mark.
And what does this question mark do.
Well it's a quantifier and it matches the previous element 0 or 1 time.
So the quantifiers are the star which matches the previous element zero or more times.
Then the plus sign which matches the previous element one or more times.
And then we have the question mark which matches the previous element 0 or 1 times.
You can also say how many times you want this to be exactly.
So matches the previous element exactly n times and for a range as well.
So at least n times but more than or but no more than M times.
So that's the different quantifiers that you can use.
So now you've seen one example of grouping.
So grouping the w.
And then one example of quantifiers by using the question mark.
So as you can see both things are still active.
So this w w w and the one without is still active.
And here it's actually w w w dot.
So this dot is included as well.
And then after the question mark I can go ahead and say tutorials dot you.
So both things are active okay.
So let's go back though we still have this little challenge to get all of those numbers to be active.
And by now you should be able to do that by what you have seen.
It's pretty tough.
I'm telling you.
It can take quite a while, maybe even half an hour or so.
Even though it's a very basic example, it can take a while to figure it out based on what you've learned
or based on the snippets that you've seen here.
So it's maybe best if you download the snippets file and look at the different character classes, character
escapes, anchors, and so forth to try it.
That's, as I said, a difficult one.
If you don't feel like a difficult challenge, then wait for the easier challenge, which will be to
find all website links or to, well, mark all website links or highlight all of them.
All right, so now you should know everything that you need in order to create patterns to find German
mobile phone numbers.
And there is one thing to consider.
So they always start with a zero or a plus 49 or A00 49, then follows a one.
So that's always the case.
So as you can see a one here, another one there and one here.
The following numbers either a six or a seven.
So there are no other numbers allowed here.
Then the next digit which is in this case the fourth digit can be any number.
Then you have a slash.
And following are just eight numbers.
And those eight numbers can be any number.
So it can be any of those ten different digits.
Okay.
So that's generally the structure.
Please try to build that if you feel like a real challenge.
It is not easy.
It can take quite a while to set it up.
But you have seen what you need to do that and well please just give it a try and go ahead and check
out the snippets to do so.
So you always go back to the snippets in order to figure it out.
And it can be difficult.
And if you feel like you're not making any progress and you can't get it to work, so can't get all
those three numbers to be highlighted, then that's fine, because I'll explain just after.
Make sure to try it.
We'll look at the solution in the next video.
See you there.