1
1

00:00:02,210  -->  00:00:03,900
<v Jose>Let's use regular expressions</v>
2

2

00:00:03,900  -->  00:00:08,060
to find a number within this string here.
3

3

00:00:08,060  -->  00:00:10,653
What to do is I'm going to import RE.
4

4

00:00:11,624  -->  00:00:15,010
And RE is the Python module for regular expressions.
5

5

00:00:15,010  -->  00:00:17,600
The regular expression allows us to
6

6

00:00:17,600  -->  00:00:21,390
define what the structure of a string is,
7

7

00:00:21,390  -->  00:00:24,530
so that we can get some information out of it.
8

8

00:00:24,530  -->  00:00:29,530
So we're gonna say that the pattern is RE dot compile.
9

9

00:00:30,210  -->  00:00:33,170
And then we're gonna use an R string.
10

10

00:00:33,170  -->  00:00:37,550
An R string in Python is used for regular expressions.
11

11

00:00:37,550  -->  00:00:39,405
And essentially, it allows us to define a string
12

12

00:00:39,405  -->  00:00:42,860
that uses regular expression syntax,
13

13

00:00:42,860  -->  00:00:44,570
and Python will sort of help us out
14

14

00:00:44,570  -->  00:00:45,883
with defining the string.
15

15

00:00:46,880  -->  00:00:50,294
Now what we want to do is tell the RE module
16

16

00:00:50,294  -->  00:00:52,010
that what we're looking for
17

17

00:00:52,010  -->  00:00:54,780
has the structure of this thing here,
18

18

00:00:54,780  -->  00:00:59,780
which is a number, a comma, a few numbers,
19

19

00:01:01,560  -->  00:01:05,490
a period, and a few more numbers.
20

20

00:01:05,490  -->  00:01:08,700
So we have that there.
21

21

00:01:08,700  -->  00:01:13,440
What we're gonna do is say backslash D.
22

22

00:01:13,440  -->  00:01:16,023
Backslash D means a number.
23

23

00:01:16,900  -->  00:01:19,270
And then we're gonna type a comma,
24

24

00:01:19,270  -->  00:01:24,180
then we're gonna type backslash D, backslash D, backslash D.
25

25

00:01:24,180  -->  00:01:27,290
Then we're gonna type backslash full stop.
26

26

00:01:27,290  -->  00:01:29,920
And then we're gonna type backslash D, backslash D.
27

27

00:01:29,920  -->  00:01:31,820
And then we're gonna close the brackets.
28

28

00:01:31,820  -->  00:01:35,820
So this is not very good regular expression,
29

29

00:01:35,820  -->  00:01:37,050
but we're gonna improve on it.
30

30

00:01:37,050  -->  00:01:38,968
It just wanna show you how it works first.
31

31

00:01:38,968  -->  00:01:40,823
We've got a number.
32

32

00:01:42,099  -->  00:01:43,540
A number, first of all, here.
33

33

00:01:43,540  -->  00:01:45,000
Then we've got a comma,
34

34

00:01:45,000  -->  00:01:46,940
and this comma does not have a backslash
35

35

00:01:46,940  -->  00:01:50,160
in front of it because the backslash, sorry, the comma
36

36

00:01:50,160  -->  00:01:53,623
doesn't mean anything special in regular expressions.
37

37

00:01:55,090  -->  00:01:57,400
The D has a backslash,
38

38

00:01:57,400  -->  00:02:00,160
because if we don't put the backslash in,
39

39

00:02:00,160  -->  00:02:02,430
what this means is that the first character
40

40

00:02:02,430  -->  00:02:04,600
should be the letter D.
41

41

00:02:04,600  -->  00:02:07,460
Backslash D means it should be a number.
42

42

00:02:07,460  -->  00:02:10,390
This is just regular expression syntax.
43

43

00:02:10,390  -->  00:02:12,820
So after the comma, we have three more numbers,
44

44

00:02:12,820  -->  00:02:14,530
three backslash Ds.
45

45

00:02:14,530  -->  00:02:16,330
And then we have a period.
46

46

00:02:16,330  -->  00:02:21,210
And we use backslash period, because if we don't,
47

47

00:02:21,210  -->  00:02:23,840
then this means that we want the
48

48

00:02:23,840  -->  00:02:26,710
period element in regular expressions.
49

49

00:02:26,710  -->  00:02:28,350
And the period in the regular expression
50

50

00:02:28,350  -->  00:02:32,110
means any character, so a number, a letter, anything else.
51

51

00:02:32,110  -->  00:02:34,530
So, if we don't put the backslash,
52

52

00:02:34,530  -->  00:02:38,360
then we could have one, something like this.
53

53

00:02:38,360  -->  00:02:41,820
These would be valued, if we don't put the backslash in,
54

54

00:02:41,820  -->  00:02:44,943
because this here is any character.
55

55

00:02:45,830  -->  00:02:49,160
If we put the backslash in, this would not be valued.
56

56

00:02:49,160  -->  00:02:51,800
It would have to be a full stop.
57

57

00:02:51,800  -->  00:02:54,873
Then we've got backslash D and backslash D, as well.
58

58

00:02:55,940  -->  00:02:59,960
Now what this means is that if we
59

59

00:02:59,960  -->  00:03:03,000
give the regular expression module
60

60

00:03:03,000  -->  00:03:07,020
this pattern and this value here,
61

61

00:03:07,020  -->  00:03:12,020
it's going to know that a part of this string
62

62

00:03:12,550  -->  00:03:14,690
is in this format.
63

63

00:03:14,690  -->  00:03:16,830
Essentially, it's going to ignore the pound sign
64

64

00:03:16,830  -->  00:03:18,920
because it doesn't really care about it,
65

65

00:03:18,920  -->  00:03:22,010
but it's going to say there is a portion of this string
66

66

00:03:22,010  -->  00:03:24,430
that has this pattern inside it.
67

67

00:03:24,430  -->  00:03:26,580
This number comma, number, number, number,
68

68

00:03:26,580  -->  00:03:28,713
full stop or period, number, number.
69

69

00:03:29,771  -->  00:03:32,778
And that is what the brackets are for.
70

70

00:03:32,778  -->  00:03:35,140
So the brackets are left until the end.
71

71

00:03:35,140  -->  00:03:36,450
What the brackets are going to do
72

72

00:03:36,450  -->  00:03:40,330
is they are going to allow us to tell
73

73

00:03:40,330  -->  00:03:42,790
the regular expression module that we are interested
74

74

00:03:42,790  -->  00:03:45,844
in just this part of the pattern.
75

75

00:03:45,844  -->  00:03:50,844
So then we can extract that as its own separate value.
76

76

00:03:52,333  -->  00:03:56,380
So, I'm gonna explain what that means in just a moment.
77

77

00:03:56,380  -->  00:03:59,610
Let's first find whether there are any matches
78

78

00:03:59,610  -->  00:04:02,970
for this pattern in our string price.
79

79

00:04:02,970  -->  00:04:05,950
The way we do that, we say pattern dot search
80

80

00:04:05,950  -->  00:04:07,883
and then we give it the string price.
81

81

00:04:09,420  -->  00:04:14,123
Okay, and then we're gonna say print match dot group,
82

82

00:04:14,960  -->  00:04:17,950
and then we're gonna give it one inside the brackets.
83

83

00:04:17,950  -->  00:04:22,030
So, let's run this and show you exactly what that does.
84

84

00:04:22,030  -->  00:04:24,300
You can see that it's printed out
85

85

00:04:24,300  -->  00:04:26,710
the thing within the string price
86

86

00:04:26,710  -->  00:04:29,646
that matches the pattern around the brackets.
87

87

00:04:29,646  -->  00:04:33,223
If we don't patient the brackets in,
88

88

00:04:34,060  -->  00:04:36,290
then this doesn't quite work,
89

89

00:04:36,290  -->  00:04:39,160
because we haven't told the regular expression module
90

90

00:04:39,160  -->  00:04:42,339
that we are interested only in this pattern here.
91

91

00:04:42,339  -->  00:04:46,740
Okay, so now what this has done
92

92

00:04:46,740  -->  00:04:48,680
is it's gone into the string price,
93

93

00:04:48,680  -->  00:04:51,430
it's found this pattern inside it,
94

94

00:04:51,430  -->  00:04:53,230
and then because we've got the brackets,
95

95

00:04:53,230  -->  00:04:56,150
it's created what's called a group for us
96

96

00:04:56,150  -->  00:04:59,840
that we can then go and retrieve if it exists.
97

97

00:04:59,840  -->  00:05:01,523
So in this case, it does exist.
98

98

00:05:02,530  -->  00:05:05,348
Now there are a couple of issues with this, though.
99

99

00:05:05,348  -->  00:05:09,700
The first issue is that there are some situations
100

100

00:05:09,700  -->  00:05:13,310
where the comma may not be there.
101

101

00:05:13,310  -->  00:05:15,850
The comma, for example, could be not there
102

102

00:05:15,850  -->  00:05:19,030
if we have a price like 567.95.
103

103

00:05:19,030  -->  00:05:21,630
There is no comma in this price.
104

104

00:05:21,630  -->  00:05:23,060
So what we can do is we can put
105

105

00:05:23,060  -->  00:05:25,990
a question mark directly after the comma
106

106

00:05:25,990  -->  00:05:30,990
and that means this is an optional thing in the string.
107

107

00:05:31,010  -->  00:05:34,470
So it will match whether there's a comma or not.
108

108

00:05:34,470  -->  00:05:37,199
So at the moment, if we have a price like this,
109

109

00:05:37,199  -->  00:05:39,210
it would match this pattern,
110

110

00:05:39,210  -->  00:05:42,260
because we've got a number, then we've got a comma,
111

111

00:05:42,260  -->  00:05:44,120
which is optional, which is not there,
112

112

00:05:44,120  -->  00:05:45,830
and then we've got three more numbers,
113

113

00:05:45,830  -->  00:05:46,960
and then we've got the period,
114

114

00:05:46,960  -->  00:05:49,118
and then we've got two more numbers.
115

115

00:05:49,118  -->  00:05:53,120
Now this pattern would not be valid for something like this,
116

116

00:05:53,120  -->  00:05:54,459
because we've got a number,
117

117

00:05:54,459  -->  00:05:56,740
then we've got a comma which is optional,
118

118

00:05:56,740  -->  00:05:57,670
then we've got two numbers,
119

119

00:05:57,670  -->  00:05:59,495
but we don't have the third number.
120

120

00:05:59,495  -->  00:06:03,440
So this backslash D would be missing.
121

121

00:06:03,440  -->  00:06:04,930
So what are we gonna do?
122

122

00:06:04,930  -->  00:06:09,930
Well, we're going to say that we want at least one number,
123

123

00:06:11,970  -->  00:06:15,113
but it can be any number of numbers.
124

124

00:06:16,580  -->  00:06:20,773
So one or more number is what this plus means.
125

125

00:06:21,740  -->  00:06:23,833
And if we do the same here,
126

126

00:06:24,960  -->  00:06:27,938
then it means we expect at least one number,
127

127

00:06:27,938  -->  00:06:32,070
then a comma, which may or may not be there,
128

128

00:06:32,070  -->  00:06:36,440
then at least one number, so that means two numbers,
129

129

00:06:36,440  -->  00:06:38,410
then we've got a period,
130

130

00:06:38,410  -->  00:06:40,403
and then we've got two more numbers.
131

131

00:06:41,650  -->  00:06:44,870
So this would match something like 15 95
132

132

00:06:44,870  -->  00:06:48,340
or one five eight 95, or indeed any other number
133

133

00:06:48,340  -->  00:06:52,513
that has at least two numbers in front of the period.
134

134

00:06:53,430  -->  00:06:55,030
We need at least two numbers,
135

135

00:06:55,030  -->  00:06:57,610
because we have the backslash D plus,
136

136

00:06:57,610  -->  00:07:00,080
which means one number or more.
137

137

00:07:00,080  -->  00:07:01,810
And we also to have backslash D plus,
138

138

00:07:01,810  -->  00:07:03,500
which means one number or more.
139

139

00:07:03,500  -->  00:07:05,513
So in total we have two numbers or more.
140

140

00:07:07,200  -->  00:07:12,200
So we can turn one of these into essentially
141

141

00:07:12,670  -->  00:07:14,663
zero or more numbers.
142

142

00:07:15,800  -->  00:07:18,363
And the way we do that is with the asterisk.
143

143

00:07:19,380  -->  00:07:24,380
So what this means is we're gonna have one or more number,
144

144

00:07:25,120  -->  00:07:28,990
a comma which is optional, and then zero or more numbers,
145

145

00:07:28,990  -->  00:07:31,290
and then a period, and then two more numbers.
146

146

00:07:31,290  -->  00:07:33,210
So now that we've got our pattern here,
147

147

00:07:33,210  -->  00:07:35,120
this may not be a perfect pattern,
148

148

00:07:35,120  -->  00:07:38,060
it maybe won't work for some currencies
149

149

00:07:38,060  -->  00:07:40,150
that use, for example, the period here
150

150

00:07:40,150  -->  00:07:41,970
and the comma here and so on.
151

151

00:07:41,970  -->  00:07:43,860
But we're not really aiming for perfection
152

152

00:07:43,860  -->  00:07:45,200
at this point in time.
153

153

00:07:45,200  -->  00:07:46,430
We can change this later on.
154

154

00:07:46,430  -->  00:07:48,190
I just wanted to show you a little about
155

155

00:07:48,190  -->  00:07:50,493
how regular expression work.
156

156

00:07:51,390  -->  00:07:52,970
And now that we have this,
157

157

00:07:52,970  -->  00:07:56,990
finding the price as a number is quite straight forward.
158

158

00:07:56,990  -->  00:07:59,993
We're gonna say found price is match group one.
159

159

00:08:00,850  -->  00:08:02,670
And then we're gonna remove the comma.
160

160

00:08:02,670  -->  00:08:04,090
Because as you can see down here,
161

161

00:08:04,090  -->  00:08:06,422
we've got the comma in this string,
162

162

00:08:06,422  -->  00:08:10,880
but we cannot turn this into a number in Python
163

163

00:08:10,880  -->  00:08:11,930
if it still has the comma.
164

164

00:08:11,930  -->  00:08:14,660
So we first have to remove the comma.
165

165

00:08:14,660  -->  00:08:17,040
And we're gonna say without commas is
166

166

00:08:17,040  -->  00:08:18,770
found price dot replace
167

167

00:08:21,480  -->  00:08:26,480
and we're going to give this replace a method two arguments.
168

168

00:08:27,080  -->  00:08:30,010
The first one is the element that we want to replace.
169

169

00:08:30,010  -->  00:08:32,980
The second one is what we want to replace it by.
170

170

00:08:32,980  -->  00:08:34,220
So what this is saying is
171

171

00:08:34,220  -->  00:08:37,550
take the commas and replace them by empty strings.
172

172

00:08:37,550  -->  00:08:39,600
Essentially just gonna delete the commas.
173

173

00:08:40,540  -->  00:08:45,540
And finally, we can say price equal float of without commas.
174

174

00:08:46,580  -->  00:08:48,546
So once we've stripped out the commas,
175

175

00:08:48,546  -->  00:08:51,550
we can turn that into a number in Python,
176

176

00:08:51,550  -->  00:08:53,900
a floating point number, in this case.
177

177

00:08:53,900  -->  00:08:56,533
And at the end, we can print the price.
178

178

00:08:57,470  -->  00:08:58,583
And we can run that.
179

179

00:08:59,530  -->  00:09:00,730
And you can see that now we get
180

180

00:09:00,730  -->  00:09:03,260
one four six nine point zero.
181

181

00:09:03,260  -->  00:09:05,534
No longer it is point zero zero,
182

182

00:09:05,534  -->  00:09:10,534
because Python knows that since you've only got one zero,
183

183

00:09:11,160  -->  00:09:13,770
once you've printed a floating point number,
184

184

00:09:13,770  -->  00:09:15,220
you no longer need the other decimal
185

185

00:09:15,220  -->  00:09:16,330
because it's also going to be zero.
186

186

00:09:16,330  -->  00:09:18,130
It's kina pointless to print it out.
187

187

00:09:19,001  -->  00:09:23,110
So this is how you would use regular expressions
188

188

00:09:23,110  -->  00:09:27,069
to extract the information contained in the string
189

189

00:09:27,069  -->  00:09:28,567
and turn it into a number.
190

190

00:09:28,567  -->  00:09:30,160
And the purpose is, of course,
191

191

00:09:30,160  -->  00:09:32,650
that we can then potentially say
192

192

00:09:32,650  -->  00:09:35,589
is this price under 1,000 pounds?
193

193

00:09:35,589  -->  00:09:38,060
And if it is, we can send the user an email
194

194

00:09:38,060  -->  00:09:39,310
and tell them about it.
195

195

00:09:39,310  -->  00:09:42,230
In this case, it's not, so, but yeah.
196

196

00:09:42,230  -->  00:09:44,470
So that's how the app is going to work.
197

197

00:09:44,470  -->  00:09:46,796
We're going to be doing this inside our app
198

198

00:09:46,796  -->  00:09:49,070
in order to calculate whether
199

199

00:09:49,070  -->  00:09:53,070
a price is under what we want or not.
200

200

00:09:53,070  -->  00:09:55,460
Of course, this is only going to be a small part of the app,
201

201

00:09:55,460  -->  00:09:57,390
but it is the main part.
202

202

00:09:57,390  -->  00:10:00,070
This is really what the app is all about
203

203

00:10:00,070  -->  00:10:02,480
and all the rest of our app is going to be there
204

204

00:10:02,480  -->  00:10:07,480 line:15% 
to allow users to very easily manage this search.
205

205

00:10:08,550  -->  00:10:10,340 line:15% 
Alright, so that's everything for this video.
206

206

00:10:10,340  -->  00:10:11,173 line:15% 
Thank you for joining me.
207

207

00:10:11,173  -->  00:10:12,520 line:15% 
I hope you've learned something in it.
208

208

00:10:12,520  -->  00:10:13,970
I'll see you in the next one.
