Hi, welcome back, this video will filter out certain URLs from a list of URLs for this I have this
year the text file, which has the input content.
You can find this file attached in the luxury sources so you can download it and you can put it in your
working directory where your Python file is.
So we have several URLs in here, and our goal for this video is to extract only the dot com you URLs,
which is the first for your URLs.
Now here we have different pattern, so we have HTP, we have HTP as we have W W W here, but not in
the other URLs.
So we have two codes for all those scenarios and only gets all the dot com URLs.
So these first four, let's go to Python and do that.
So first of all, I need to load this content as a variable.
I'll use that with context manager for that and the open function, which expects the path to the file
you are to open this in remote s file and then create a variable content file that reads.
Now if I print out contents here.
And execute with command, enter or control enter, you will get the contents or that's yours that you
know that if you just print outs just like that, so without using print, you'll get a less formatted
version, so the brake lights will show as a row backslash and symbols.
So it's better to use print contents to view the outputs in a nicer format so that now we can build
a regular expression pattern.
So let's import our in the library, and of course, first we need to create a pattern to build a pattern
using the compile methods.
So there goes the string that contains the pattern.
Whenever you want to build a pattern, you want to first think to have an understanding of how your
daughter is constructed.
So in our case, as we said, we have HTP, we have HTP as we have W.W. here.
So let's not forget those alterations in our units.
So let's start, we always have HTP, though, so that would be a literal string, then sometimes we
do have an s and sometimes we don't.
So we have different protocols, actually be an asset base to account for both cases.
You want to write s and then a question mark, why the question mark?
Well, because question mark esubmitter character and you so that's it matched a preceding element zero
or one time.
That means this pattern will match a city p.
And it will also match a city as because as is occurring zero times in the P.
And one time in A. Yes.
So both these two are included in the match.
So I hope that makes sense.
Then we always have a column after the P or after the S, then we always have two slashes, but then
we sometimes we have W W W and sometimes we don't.
For this optional W W W dot parts, we would use parentheses and a question mark and a column.
So that is similar to the old pattern or operator.
The difference here is that we don't use that's a vertical bore, but we simply put the characters that
we want to optionally match, which is w w dot.
So only the Wikipedia URL has that WW dot, you see, the other ones don't have it, so that's better
makes sure to filter optional cases.
But this also needs the question mark afterwards.
So that is actually the most complicated part of this regular expression.
And that's something you need to memorize.
So it's a code for optional characters.
Then we have the other ports.
So the main body of the domain that supports Wikipedia, example Google.
So all these seem to be letters, but sometimes we have dashes in that spot, so we have to count for
those characters as well.
So I think it's better to use this major character here.
So to include anything but space and we want to do it one or more times.
So we want to capture that with the plus one or more times than we have a dot here.
Everything has a dot com, actually, but remember that the Dot is a mental character, so we would
want to escape it to be able to treat it as a normal dot character.
So let's get some matches now from pattern using Find All, and the content is the variable that contains
the text we want to filter.
And let's print all the matches here as outputs.
So we see we got HTP, Google Dot com backslash and as a break line.
Then we have example dot com.
We have Wikipedia dot com and we have Python dot com.
So we got everything we wanted.
However, I do see a small issue here.
The expected outputs of the phone's old methods is a list of several items several strings where each
string is one match, as it was the case in the previous example.
You see, this list had two items this one and that on both these lists has only one.
You see, it's one string.
And the matches are divided by this backslash, and we can post-process this string to extract the somewhere
else, but we can do better and we can fix this.
The regular expression because we're missing something here.
The problem is here in here, what we did is we excluded spaces.
So we are working on the body of the domain of this expression here, which includes everything but
spaces.
However, in this world, we also have this backslash end, which is also a symbol.
So we also need to negate those symbols.
So backslash, and that means this will not be treated as one single stream.
No, because that's what Python did.
It's continued searching for those non-white spaces.
So if I execute this now, we should get better results.
So there is one item here.
Another item to three.
So we have four items at this time.
So that is the final regular expression that the struggles that the dots call murals from this file
here.
Let's do some more examples with regular expressions, particularly in the next video, we'll be working
on filtering out text that has numbers so IP addresses to be exact.
I'll talk to you in the next video.