1
00:00:00,210 --> 00:00:09,840
Hi, welcome back, this video will filter out certain URLs from a list of URLs for this I have this

2
00:00:09,840 --> 00:00:13,530
year the text file, which has the input content.

3
00:00:14,010 --> 00:00:19,770
You can find this file attached in the luxury sources so you can download it and you can put it in your

4
00:00:19,800 --> 00:00:22,710
working directory where your Python file is.

5
00:00:24,020 --> 00:00:34,460
So we have several URLs in here, and our goal for this video is to extract only the dot com you URLs,

6
00:00:34,910 --> 00:00:38,360
which is the first for your URLs.

7
00:00:38,780 --> 00:00:48,650
Now here we have different pattern, so we have HTP, we have HTP as we have W W W here, but not in

8
00:00:48,650 --> 00:00:49,730
the other URLs.

9
00:00:50,180 --> 00:00:56,870
So we have two codes for all those scenarios and only gets all the dot com URLs.

10
00:00:58,100 --> 00:01:02,630
So these first four, let's go to Python and do that.

11
00:01:03,260 --> 00:01:07,550
So first of all, I need to load this content as a variable.

12
00:01:08,390 --> 00:01:16,370
I'll use that with context manager for that and the open function, which expects the path to the file

13
00:01:16,370 --> 00:01:24,680
you are to open this in remote s file and then create a variable content file that reads.

14
00:01:25,250 --> 00:01:27,650
Now if I print out contents here.

15
00:01:29,530 --> 00:01:36,610
And execute with command, enter or control enter, you will get the contents or that's yours that you

16
00:01:37,600 --> 00:01:45,670
know that if you just print outs just like that, so without using print, you'll get a less formatted

17
00:01:45,670 --> 00:01:51,800
version, so the brake lights will show as a row backslash and symbols.

18
00:01:52,120 --> 00:02:01,030
So it's better to use print contents to view the outputs in a nicer format so that now we can build

19
00:02:01,030 --> 00:02:03,430
a regular expression pattern.

20
00:02:05,120 --> 00:02:14,240
So let's import our in the library, and of course, first we need to create a pattern to build a pattern

21
00:02:14,240 --> 00:02:15,680
using the compile methods.

22
00:02:16,280 --> 00:02:18,860
So there goes the string that contains the pattern.

23
00:02:20,210 --> 00:02:29,540
Whenever you want to build a pattern, you want to first think to have an understanding of how your

24
00:02:29,540 --> 00:02:32,180
daughter is constructed.

25
00:02:32,720 --> 00:02:39,460
So in our case, as we said, we have HTP, we have HTP as we have W.W. here.

26
00:02:39,470 --> 00:02:45,650
So let's not forget those alterations in our units.

27
00:02:47,000 --> 00:02:56,300
So let's start, we always have HTP, though, so that would be a literal string, then sometimes we

28
00:02:56,300 --> 00:02:59,150
do have an s and sometimes we don't.

29
00:02:59,660 --> 00:03:06,330
So we have different protocols, actually be an asset base to account for both cases.

30
00:03:06,350 --> 00:03:11,290
You want to write s and then a question mark, why the question mark?

31
00:03:11,300 --> 00:03:20,510
Well, because question mark esubmitter character and you so that's it matched a preceding element zero

32
00:03:20,510 --> 00:03:21,830
or one time.

33
00:03:22,310 --> 00:03:27,080
That means this pattern will match a city p.

34
00:03:28,090 --> 00:03:36,370
And it will also match a city as because as is occurring zero times in the P.

35
00:03:36,880 --> 00:03:39,100
And one time in A. Yes.

36
00:03:39,400 --> 00:03:43,090
So both these two are included in the match.

37
00:03:43,720 --> 00:03:45,490
So I hope that makes sense.

38
00:03:46,060 --> 00:03:56,500
Then we always have a column after the P or after the S, then we always have two slashes, but then

39
00:03:56,500 --> 00:04:01,150
we sometimes we have W W W and sometimes we don't.

40
00:04:02,110 --> 00:04:14,230
For this optional W W W dot parts, we would use parentheses and a question mark and a column.

41
00:04:14,410 --> 00:04:18,820
So that is similar to the old pattern or operator.

42
00:04:19,420 --> 00:04:27,220
The difference here is that we don't use that's a vertical bore, but we simply put the characters that

43
00:04:27,340 --> 00:04:31,750
we want to optionally match, which is w w dot.

44
00:04:32,230 --> 00:04:39,400
So only the Wikipedia URL has that WW dot, you see, the other ones don't have it, so that's better

45
00:04:39,400 --> 00:04:42,850
makes sure to filter optional cases.

46
00:04:43,690 --> 00:04:47,110
But this also needs the question mark afterwards.

47
00:04:47,680 --> 00:04:52,360
So that is actually the most complicated part of this regular expression.

48
00:04:52,900 --> 00:04:54,700
And that's something you need to memorize.

49
00:04:55,060 --> 00:04:58,150
So it's a code for optional characters.

50
00:04:58,360 --> 00:05:01,880
Then we have the other ports.

51
00:05:02,240 --> 00:05:08,470
So the main body of the domain that supports Wikipedia, example Google.

52
00:05:08,740 --> 00:05:17,170
So all these seem to be letters, but sometimes we have dashes in that spot, so we have to count for

53
00:05:17,410 --> 00:05:18,670
those characters as well.

54
00:05:19,090 --> 00:05:24,010
So I think it's better to use this major character here.

55
00:05:24,310 --> 00:05:31,330
So to include anything but space and we want to do it one or more times.

56
00:05:31,330 --> 00:05:39,850
So we want to capture that with the plus one or more times than we have a dot here.

57
00:05:40,570 --> 00:05:46,690
Everything has a dot com, actually, but remember that the Dot is a mental character, so we would

58
00:05:46,690 --> 00:05:52,840
want to escape it to be able to treat it as a normal dot character.

59
00:05:53,230 --> 00:06:01,240
So let's get some matches now from pattern using Find All, and the content is the variable that contains

60
00:06:01,240 --> 00:06:02,620
the text we want to filter.

61
00:06:03,550 --> 00:06:07,300
And let's print all the matches here as outputs.

62
00:06:07,870 --> 00:06:14,530
So we see we got HTP, Google Dot com backslash and as a break line.

63
00:06:14,530 --> 00:06:16,780
Then we have example dot com.

64
00:06:16,780 --> 00:06:20,500
We have Wikipedia dot com and we have Python dot com.

65
00:06:20,770 --> 00:06:22,750
So we got everything we wanted.

66
00:06:23,380 --> 00:06:26,500
However, I do see a small issue here.

67
00:06:27,280 --> 00:06:36,370
The expected outputs of the phone's old methods is a list of several items several strings where each

68
00:06:36,370 --> 00:06:41,860
string is one match, as it was the case in the previous example.

69
00:06:41,860 --> 00:06:49,910
You see, this list had two items this one and that on both these lists has only one.

70
00:06:49,930 --> 00:06:51,520
You see, it's one string.

71
00:06:52,570 --> 00:07:02,200
And the matches are divided by this backslash, and we can post-process this string to extract the somewhere

72
00:07:02,200 --> 00:07:06,910
else, but we can do better and we can fix this.

73
00:07:07,210 --> 00:07:10,120
The regular expression because we're missing something here.

74
00:07:10,540 --> 00:07:17,290
The problem is here in here, what we did is we excluded spaces.

75
00:07:17,920 --> 00:07:25,390
So we are working on the body of the domain of this expression here, which includes everything but

76
00:07:25,480 --> 00:07:26,350
spaces.

77
00:07:26,710 --> 00:07:32,260
However, in this world, we also have this backslash end, which is also a symbol.

78
00:07:32,710 --> 00:07:38,920
So we also need to negate those symbols.

79
00:07:39,190 --> 00:07:44,510
So backslash, and that means this will not be treated as one single stream.

80
00:07:44,530 --> 00:07:47,130
No, because that's what Python did.

81
00:07:47,140 --> 00:07:51,700
It's continued searching for those non-white spaces.

82
00:07:52,090 --> 00:07:56,100
So if I execute this now, we should get better results.

83
00:07:56,110 --> 00:07:57,970
So there is one item here.

84
00:07:58,660 --> 00:08:01,750
Another item to three.

85
00:08:01,750 --> 00:08:03,670
So we have four items at this time.

86
00:08:04,870 --> 00:08:12,940
So that is the final regular expression that the struggles that the dots call murals from this file

87
00:08:12,940 --> 00:08:13,360
here.

88
00:08:17,400 --> 00:08:23,550
Let's do some more examples with regular expressions, particularly in the next video, we'll be working

89
00:08:23,550 --> 00:08:30,120
on filtering out text that has numbers so IP addresses to be exact.

90
00:08:30,450 --> 00:08:31,650
I'll talk to you in the next video.

