1
00:00:00,150 --> 00:00:08,100
In this video, we'll be using regular expressions to extract the email addresses from this string.

2
00:00:08,640 --> 00:00:10,890
So let's tour the string in a variable.

3
00:00:12,120 --> 00:00:18,030
Let's say this is the text variable execute that cell with control, enter or command.

4
00:00:18,030 --> 00:00:26,430
Enter a then create a new code cell using this button or control G or Command G.

5
00:00:27,870 --> 00:00:32,310
And then here we want to write the regular expression code.

6
00:00:32,880 --> 00:00:39,690
Now, to use irregular expressions, we use the R e-library, which stands for regular.

7
00:00:40,440 --> 00:00:41,790
Then it goes like this.

8
00:00:42,000 --> 00:00:47,790
You create a pattern objects using R e dot compile.

9
00:00:49,170 --> 00:00:56,370
Now remember, I took the boat, a set of special characters that we use for regular expressions, and

10
00:00:56,370 --> 00:01:03,600
we write those characters inside these quotes here in the compiled methods.

11
00:01:04,110 --> 00:01:13,170
So we want to extract only the email addresses, so the final outputs will be a listes containing these

12
00:01:13,170 --> 00:01:14,550
two email addresses.

13
00:01:14,820 --> 00:01:21,660
Now, when you write a regular expression, you first have to understand the pattern of a string pattern

14
00:01:22,170 --> 00:01:26,580
that's the output string has.

15
00:01:26,680 --> 00:01:34,050
So what pattern do we have here with these output strings that we want to extract?

16
00:01:34,830 --> 00:01:40,020
So we have characters than we have at this symbol.

17
00:01:40,410 --> 00:01:47,970
And then we have some more characters which could be column D or other domain extensions.

18
00:01:48,900 --> 00:01:51,360
So we start from here.

19
00:01:52,470 --> 00:01:55,950
We could have any range of letters.

20
00:01:56,970 --> 00:02:02,310
Let's suppose for now, that email's only contains letters.

21
00:02:02,520 --> 00:02:09,539
Let's consider the fact that they could also be dashes in the email or underscores or.

22
00:02:09,870 --> 00:02:10,440
And so on.

23
00:02:10,770 --> 00:02:15,540
We're going to talk about that in just a minute about those characters.

24
00:02:15,840 --> 00:02:23,040
So if there were only characters, you'd see something like square brackets and the square brackets

25
00:02:23,490 --> 00:02:27,000
puts a range from A to Z.

26
00:02:27,690 --> 00:02:32,880
Then if you want a real character, not a special character.

27
00:02:33,150 --> 00:02:42,390
So these are special characters eats like a language by using those square brackets, and that's a dash.

28
00:02:42,390 --> 00:02:46,770
That's you say, give me any characters from A to Z.

29
00:02:46,980 --> 00:02:48,870
And then you use it.

30
00:02:49,560 --> 00:02:55,920
So without square brackets, because it's a literal character, you want to get all those that have

31
00:02:55,920 --> 00:02:56,280
it.

32
00:02:56,790 --> 00:03:01,320
Then let's say again A to Z, then dot.

33
00:03:01,980 --> 00:03:11,520
So a literal a normal string for dots there and then more a to set characters.

34
00:03:12,150 --> 00:03:14,940
Once you have created your pattern.

35
00:03:15,210 --> 00:03:18,880
So this is the regular expression or pattern.

36
00:03:18,900 --> 00:03:26,010
In other words, then you create another variable that's named this matches.

37
00:03:26,880 --> 00:03:30,600
So what string matches we're going to have with output strings?

38
00:03:31,430 --> 00:03:42,810
Then you point to that pattern that you created there and then use the find all methods finds all gets

39
00:03:42,810 --> 00:03:44,100
as arguments.

40
00:03:44,110 --> 00:03:45,270
Can you guess what?

41
00:03:45,900 --> 00:03:47,220
Well, the text variable.

42
00:03:47,220 --> 00:03:51,030
So the text there bring prindle matches.

43
00:03:51,270 --> 00:03:58,550
So in on Jupiter, you don't need to use print matches if the variable is the last one in the cell.

44
00:03:58,560 --> 00:04:02,190
So you can just do matches like that's executed with comments.

45
00:04:02,490 --> 00:04:04,470
Enter or control enter.

46
00:04:05,250 --> 00:04:07,080
And this is the output.

47
00:04:08,610 --> 00:04:11,240
Now we are close, but we are not there yet.

48
00:04:11,250 --> 00:04:16,920
So you see that we didn't manage to extract the complete email addresses.

49
00:04:18,459 --> 00:04:21,519
That is because when we do this.

50
00:04:22,120 --> 00:04:25,990
So this part of the pattern sees that.

51
00:04:26,770 --> 00:04:32,190
Give me one single a letter from A to Z.

52
00:04:32,770 --> 00:04:38,260
We're not specifying how many letters this expressions should match.

53
00:04:38,770 --> 00:04:48,610
So we want to fix that by adding gloss here with class, you see, detect or match any letters from

54
00:04:48,610 --> 00:04:51,830
A to Z one or more times.

55
00:04:51,880 --> 00:04:55,330
So it could be one letter, two letters, three letters and so on.

56
00:04:55,660 --> 00:05:02,410
Let's suppose there are seven letters matched or one two three four five six seven.

57
00:05:02,410 --> 00:05:03,910
Yes, seven letters.

58
00:05:04,360 --> 00:05:06,850
So one or more seven is one or more.

59
00:05:06,850 --> 00:05:08,500
So we get seven letters.

60
00:05:08,500 --> 00:05:10,540
Then we have this at here.

61
00:05:11,260 --> 00:05:18,310
So up to here, if I execute this again, now you see, we're getting closer.

62
00:05:18,790 --> 00:05:22,030
We got all the letters before ET.

63
00:05:22,450 --> 00:05:27,250
But we want to add also here a clause in the second group.

64
00:05:27,730 --> 00:05:33,520
So after the ETS, we also want one or more letters, so let's execute again.

65
00:05:34,540 --> 00:05:44,830
Yes, so example at example, so one or more letters at one or more letters than we have a dot and then

66
00:05:44,830 --> 00:05:46,990
we have one or more letters again.

67
00:05:47,470 --> 00:05:49,180
So plus of the answer also.

68
00:05:50,740 --> 00:05:52,450
And so there we go.

69
00:05:52,480 --> 00:05:59,230
We got two email addresses, so that is an example of a regular expression.

70
00:05:59,980 --> 00:06:03,630
However, we didn't consider all the scenarios here.

71
00:06:03,640 --> 00:06:06,580
So what if we have this underscore here?

72
00:06:07,120 --> 00:06:11,590
Let's execute that cell with the variable is defined, execute the other.

73
00:06:11,600 --> 00:06:16,060
So and you see that we didn't get the entire.

74
00:06:17,200 --> 00:06:19,510
Email address for the first occurrence.

75
00:06:20,350 --> 00:06:29,620
So it started with the underscore ends m p l e m p l e dot example, that's com.

76
00:06:30,160 --> 00:06:40,450
So what the surge is doing here is that it goes to all the characters, one after the other from the

77
00:06:40,450 --> 00:06:41,350
start of a string.

78
00:06:42,100 --> 00:06:44,860
It looks for one or more characters.

79
00:06:45,760 --> 00:06:49,300
So there are one or more characters here, from A to Z.

80
00:06:49,810 --> 00:06:50,630
That's fine.

81
00:06:50,860 --> 00:06:58,990
The company, all methods will start to include the screen, but then it finds out that after the letters,

82
00:06:58,990 --> 00:07:00,520
there's no ET.

83
00:07:00,910 --> 00:07:05,800
So it does not match the this string, then it continues with that.

84
00:07:06,100 --> 00:07:09,430
It does not matter then because there's no ATS offer.

85
00:07:09,430 --> 00:07:11,430
That's and then it goes here.

86
00:07:11,440 --> 00:07:15,040
It does not match those letters from.

87
00:07:15,310 --> 00:07:20,860
So from A to Z, it does not match those because they don't enter with this on the score.

88
00:07:21,610 --> 00:07:28,090
And then it looks at M P, L E and we have this ad.

89
00:07:28,120 --> 00:07:29,290
So it matches.

90
00:07:29,290 --> 00:07:30,340
That's that's also.

91
00:07:31,510 --> 00:07:34,610
And then it matches the other strings.

92
00:07:35,080 --> 00:07:41,830
So A to Z score one or more times, and then it sees that we have a dot, so it matches the dots as

93
00:07:41,830 --> 00:07:42,070
well.

94
00:07:42,610 --> 00:07:44,590
And then it matches the lost boards.

95
00:07:45,580 --> 00:07:50,670
So even if we had something like at blah blah.

96
00:07:50,980 --> 00:07:52,780
So that is not an email address.

97
00:07:53,170 --> 00:07:56,530
So I'm going to execute that cell, execute that as well.

98
00:07:56,710 --> 00:08:04,060
You see that this is not matched because the entire expression should be matched.

99
00:08:04,330 --> 00:08:07,060
And this does not have something before.

100
00:08:07,270 --> 00:08:09,250
It does not have letters before the ATS.

101
00:08:09,640 --> 00:08:14,020
So these letters here are not before that's at.

102
00:08:14,900 --> 00:08:17,200
Therefore, it's a negative outcome.

103
00:08:18,100 --> 00:08:22,990
So back to this under sort of characters, how can we get this email address also?

104
00:08:23,350 --> 00:08:26,080
Well, we have to change the way we think now.

105
00:08:26,860 --> 00:08:36,090
So what we really want to catch here is any character except whitespace for that.

106
00:08:36,090 --> 00:08:39,760
So we need to use this expression.

107
00:08:40,390 --> 00:08:47,860
So this symbol and a space, of course, between these opening and closing square brackets.

108
00:08:48,100 --> 00:08:52,180
So what this says is basically this symbol is a negation.

109
00:08:52,180 --> 00:09:00,970
It sees not space, so match any number of characters except space.

110
00:09:01,330 --> 00:09:04,600
Therefore, the compile methods will look at the string.

111
00:09:04,990 --> 00:09:11,890
It will see we have this year, so no space here, but there's no ads after that.

112
00:09:11,890 --> 00:09:13,330
So it ignores that.

113
00:09:13,810 --> 00:09:15,790
Then there's a space where it ignores that.

114
00:09:15,790 --> 00:09:17,260
And so it goes here.

115
00:09:17,950 --> 00:09:23,050
It sees that we have any character, but space.

116
00:09:23,050 --> 00:09:24,490
So it goes on.

117
00:09:24,490 --> 00:09:25,180
We have ads.

118
00:09:25,420 --> 00:09:30,810
We have also these letters, which we also need to improve by using the same pattern.

119
00:09:30,820 --> 00:09:35,050
So any character, but not the space, the white space.

120
00:09:35,410 --> 00:09:38,740
Now should we use the same for the lost parts?

121
00:09:38,950 --> 00:09:47,550
So for the domain extension, I don't think so because we don't have characters over the letters in,

122
00:09:47,560 --> 00:09:48,780
that's the ports.

123
00:09:49,150 --> 00:09:53,170
So in that case, a dash set is fine.

124
00:09:53,950 --> 00:10:02,290
So I think this is the final code that instructs all the email addresses from this string here.

125
00:10:02,290 --> 00:10:10,390
And you see that we have a list, a python list made or different items, and each item is a string.

126
00:10:11,380 --> 00:10:15,580
What you do with this list of the words is not my business.

127
00:10:15,850 --> 00:10:22,510
So if you could send an email address to those, you could convert them into a pandas dataframe and

128
00:10:22,510 --> 00:10:24,040
save them into a Caesar file.

129
00:10:24,280 --> 00:10:26,020
It's up to your project's needs.

130
00:10:26,410 --> 00:10:31,720
So this was an example to get you started with regular expressions.

131
00:10:32,170 --> 00:10:36,220
Now, I know you're still confused with what's use and when to use words.

132
00:10:36,760 --> 00:10:43,750
So in the next video, I'm going, I'm going to give you a list of the metal characters that you can

133
00:10:43,750 --> 00:10:45,430
use with regular expressions.

134
00:10:45,760 --> 00:10:48,640
So all these are metal characters.

135
00:10:49,060 --> 00:10:52,480
So we have metal characters and we have normal characters.

136
00:10:52,750 --> 00:10:57,400
So this ETS is treated as a normal character, right?

137
00:10:57,400 --> 00:11:04,750
Because it's a literal match and these other ones are metal characters, so eight z and so on.

138
00:11:05,230 --> 00:11:10,210
So I'm going to give you a complete list of these characters in the next video.

139
00:11:10,300 --> 00:11:10,810
See you there!

