WEBVTT 1 00:00:00.200 --> 00:00:01.510 Hi, welcome back. 2 00:00:01.510 --> 00:00:05.070 We've just looked at some regular expression examples. 3 00:00:05.070 --> 00:00:06.570 We've not going very in depth, 4 00:00:06.570 --> 00:00:08.820 but as we progress through the section, 5 00:00:08.820 --> 00:00:11.150 we're going to learn a little bit more about it. 6 00:00:11.150 --> 00:00:13.080 And as we do more Python 7 00:00:13.080 --> 00:00:15.400 and we encounter more uses for regular expressions, 8 00:00:15.400 --> 00:00:17.100 we're going to learn some more. 9 00:00:17.100 --> 00:00:18.750 Regular expressions is one of those things, 10 00:00:18.750 --> 00:00:21.330 that you use it, you learn about it 11 00:00:21.330 --> 00:00:24.020 and then you just don't find any use for it for a while, 12 00:00:24.020 --> 00:00:25.230 and then you just sort of forget, 13 00:00:25.230 --> 00:00:26.063 and then you have to go back 14 00:00:26.063 --> 00:00:26.950 and learn it again. 15 00:00:26.950 --> 00:00:29.970 So it's not that great if I just spend a few hours 16 00:00:29.970 --> 00:00:32.750 to show you all there is to know about regular expressions. 17 00:00:32.750 --> 00:00:34.230 I think you'll forget quickly, 18 00:00:34.230 --> 00:00:36.810 I don't think it'll bring you that much benefit. 19 00:00:36.810 --> 00:00:40.360 When you want to use regular expressions within Python, 20 00:00:40.360 --> 00:00:41.650 you can do that. 21 00:00:41.650 --> 00:00:44.524 You can import the RE module. 22 00:00:44.524 --> 00:00:46.630 RE stands for regular expressions, 23 00:00:46.630 --> 00:00:47.900 and it comes with Python, 24 00:00:47.900 --> 00:00:52.090 and it lets you test an expression against some text. 25 00:00:52.090 --> 00:00:54.660 That's essentially what we did in the last video. 26 00:00:54.660 --> 00:00:57.030 So this will be our text, it'll be an email 27 00:00:59.330 --> 00:01:02.453 and this will be our expression that we are testing, 28 00:01:03.550 --> 00:01:05.233 A to Z plus. 29 00:01:06.590 --> 00:01:09.560 What is this going to match? 30 00:01:09.560 --> 00:01:12.853 The A to Z plus would match the Jose, 31 00:01:13.760 --> 00:01:16.060 it would match the tecladocode, 32 00:01:16.060 --> 00:01:18.140 and it will match the com 33 00:01:18.140 --> 00:01:20.800 because there are three separate words in here 34 00:01:20.800 --> 00:01:22.820 and the a to z plus, what it's measuring is 35 00:01:22.820 --> 00:01:25.173 or what it's testing is any letter, 36 00:01:26.670 --> 00:01:28.189 at least one 37 00:01:28.189 --> 00:01:29.993 and as many as possible. 38 00:01:31.160 --> 00:01:31.993 Okay? 39 00:01:33.230 --> 00:01:35.080 So what we'll do is we'll say matches 40 00:01:36.710 --> 00:01:38.737 is re.findall 41 00:01:40.210 --> 00:01:41.980 expression on email. 42 00:01:41.980 --> 00:01:44.170 What that's going to do is going to take this expression, 43 00:01:44.170 --> 00:01:46.600 and it's going to try to find all the instances 44 00:01:46.600 --> 00:01:48.790 in which it occurs, so it's going to find three. 45 00:01:48.790 --> 00:01:50.860 Jose, tecladocode 46 00:01:50.860 --> 00:01:52.120 and com. 47 00:01:52.120 --> 00:01:53.370 Okay? 48 00:01:53.370 --> 00:01:55.350 Let's print out the matches, 49 00:01:55.350 --> 00:01:58.000 then we're gonna say a couple of things here. 50 00:01:58.000 --> 00:01:58.993 Let's run it first. 51 00:02:01.120 --> 00:02:05.440 Notice how our matches are now turned into a list 52 00:02:05.440 --> 00:02:07.910 of Jose, tecladocode 53 00:02:07.910 --> 00:02:08.743 and com. 54 00:02:08.743 --> 00:02:10.770 So that's all the matches that we've found 55 00:02:10.770 --> 00:02:11.970 in this expression here. 56 00:02:13.080 --> 00:02:14.560 So that's pretty cool. 57 00:02:14.560 --> 00:02:18.920 As we can see, the name is matches zero, that's my name 58 00:02:18.920 --> 00:02:22.020 and the domain tecladocode.com 59 00:02:22.020 --> 00:02:26.353 could be made of matches one dot matches two, 60 00:02:28.710 --> 00:02:30.257 like so. 61 00:02:30.257 --> 00:02:32.925 So that's tecladocode, that's the dot 62 00:02:32.925 --> 00:02:35.300 and that's the com. 63 00:02:35.300 --> 00:02:39.530 So in total we make up tecladocode.com, 64 00:02:39.530 --> 00:02:42.480 but of course this is wrong because it has to be like that. 65 00:02:42.480 --> 00:02:44.260 My bad, it has to be like that. 66 00:02:44.260 --> 00:02:47.500 So, then we can print the name, we can print the domain. 67 00:02:47.500 --> 00:02:49.100 Let's run that. 68 00:02:49.100 --> 00:02:51.350 And you'll see that this all seems fine. 69 00:02:51.350 --> 00:02:52.550 We've got Jose 70 00:02:52.550 --> 00:02:54.113 and we've got tecladocode.com. 71 00:02:56.840 --> 00:02:59.290 Now a better way of extracting the name 72 00:02:59.290 --> 00:03:01.090 and the domain 73 00:03:01.090 --> 00:03:03.140 would be to modify this expression 74 00:03:03.140 --> 00:03:05.280 in order to make it A to Z 75 00:03:05.280 --> 00:03:06.283 and include the dot, 76 00:03:07.210 --> 00:03:10.270 because now there are two matches. 77 00:03:10.270 --> 00:03:14.770 Jose, because that matches four characters in this range, 78 00:03:14.770 --> 00:03:17.520 so you've got A to Z or the dot, 79 00:03:17.520 --> 00:03:20.260 and you've got four, or one or more of those 80 00:03:20.260 --> 00:03:22.150 and you have four characters, 81 00:03:22.150 --> 00:03:23.800 and you've got tecladocode.com, 82 00:03:23.800 --> 00:03:28.800 because that matches a character or a dot many times 83 00:03:29.210 --> 00:03:31.790 and you've got there a lot of matches. 84 00:03:31.790 --> 00:03:33.410 So now you're gonna have two matches. 85 00:03:33.410 --> 00:03:34.243 Jose 86 00:03:34.243 --> 00:03:35.950 and tecladocode.com. 87 00:03:35.950 --> 00:03:40.690 So the domain is going to be matches one. 88 00:03:40.690 --> 00:03:41.590 Let's have a look. 89 00:03:43.100 --> 00:03:43.933 And there you have it. 90 00:03:43.933 --> 00:03:45.377 So now you've got two matches, Jose 91 00:03:45.377 --> 00:03:47.350 and tecladocode.com. 92 00:03:47.350 --> 00:03:48.940 Jose is the first match, 93 00:03:48.940 --> 00:03:50.740 tecladocode.com is the second match. 94 00:03:53.070 --> 00:03:54.630 Of course, if you did wanna do that, 95 00:03:54.630 --> 00:03:57.080 if you wanted to extract the domain of an email 96 00:03:57.080 --> 00:03:58.420 the best thing you can do is 97 00:03:58.420 --> 00:04:00.370 you can not do any of that 98 00:04:00.370 --> 00:04:02.370 and instead you can have your email here 99 00:04:04.460 --> 00:04:07.560 and you can have something like parts 100 00:04:07.560 --> 00:04:11.140 is email.split on the @ symbol. 101 00:04:11.140 --> 00:04:12.660 We've looked at the split method before, 102 00:04:12.660 --> 00:04:13.940 that does exactly what we need. 103 00:04:13.940 --> 00:04:17.330 So we either need regular expressions for this et all. 104 00:04:17.330 --> 00:04:21.500 So again, the name would be parts zero, 105 00:04:21.500 --> 00:04:24.970 the domain would be parts one, in this case 106 00:04:24.970 --> 00:04:26.320 and you can print them out. 107 00:04:29.290 --> 00:04:32.462 So in some cases, you won't need regular expressions at all 108 00:04:32.462 --> 00:04:35.000 and it's important to recognise those instances. 109 00:04:35.000 --> 00:04:36.460 If you just wanted to get the name 110 00:04:36.460 --> 00:04:38.740 and the domain of an email, 111 00:04:38.740 --> 00:04:41.423 you don't need regex, you can just do it with Python. 112 00:04:42.730 --> 00:04:45.510 Here's a slightly more contrived example. 113 00:04:45.510 --> 00:04:48.060 Introducing a couple of new concepts 114 00:04:48.060 --> 00:04:50.860 that we didn't look at in the last video. 115 00:04:50.860 --> 00:04:54.520 So we're gonna import RE for the Regular Expression Module 116 00:04:54.520 --> 00:04:56.870 and then this is gonna be our text. 117 00:04:56.870 --> 00:04:57.860 Price is 118 00:04:58.727 --> 00:05:00.603 $189.50. 119 00:05:01.920 --> 00:05:03.733 So what's our expression gonna be? 120 00:05:05.750 --> 00:05:07.780 Well I'll let you think about that for a wee bit 121 00:05:07.780 --> 00:05:11.143 and I'm just gonna check our matches here. 122 00:05:15.000 --> 00:05:18.110 And notice how I'm now doing RE.search. 123 00:05:18.110 --> 00:05:19.370 What this is going to do is it's going to do 124 00:05:19.370 --> 00:05:21.353 something slightly different. 125 00:05:24.620 --> 00:05:26.970 I'll tell you in just a moment what it's doing. 126 00:05:28.750 --> 00:05:30.340 So let's say that our Regular Expression 127 00:05:30.340 --> 00:05:32.953 is going to be 189.50. 128 00:05:35.590 --> 00:05:37.823 What do you think we're going to find? 129 00:05:41.160 --> 00:05:42.230 Let's run this. 130 00:05:44.137 --> 00:05:47.940 And notice how now we get 189.50 as matches dot group zero 131 00:05:50.960 --> 00:05:53.590 and we get an index error no such group 132 00:05:53.590 --> 00:05:55.240 as the second group. 133 00:05:55.240 --> 00:05:58.260 Okay, so something's going on here, 134 00:05:58.260 --> 00:06:00.650 it's only able to get one group, 135 00:06:00.650 --> 00:06:05.650 so presumably group zero is the thing that's matched. 136 00:06:07.560 --> 00:06:08.860 And that would be correct. 137 00:06:10.000 --> 00:06:13.400 Now I'm going to modify my expressions slightly 138 00:06:13.400 --> 00:06:14.820 to include the price 139 00:06:16.450 --> 00:06:19.043 and let's include the dollar sign here. 140 00:06:20.350 --> 00:06:24.390 But actually this pattern here is not gonna match 141 00:06:24.390 --> 00:06:26.700 this text because the dollar means something 142 00:06:26.700 --> 00:06:28.290 in regular expressions. 143 00:06:28.290 --> 00:06:30.850 So we have to put a backslash in front 144 00:06:30.850 --> 00:06:33.700 to make sure that this gets treated as the dollar sign 145 00:06:33.700 --> 00:06:37.180 and not as what it means in regular expressions. 146 00:06:37.180 --> 00:06:38.847 So let's press on. 147 00:06:38.847 --> 00:06:41.320 Now there is so we can see that the entire thing 148 00:06:41.320 --> 00:06:46.320 is the group zero, price colon space dollar 189.50. 149 00:06:48.250 --> 00:06:51.680 Now I'm gonna do something that's gonna be a bit weird 150 00:06:51.680 --> 00:06:53.770 but I'm gonna put some brackets in here 151 00:06:53.770 --> 00:06:55.890 around the 189.50. 152 00:06:55.890 --> 00:06:57.660 So I've just put some brackets 153 00:06:58.730 --> 00:06:59.563 and that's it. 154 00:07:00.610 --> 00:07:01.710 Now lets run it again. 155 00:07:03.260 --> 00:07:06.200 Notice how now we don't get an error. 156 00:07:06.200 --> 00:07:10.450 We get price is $189.50 157 00:07:10.450 --> 00:07:14.273 and now the group one is 189.50. 158 00:07:15.600 --> 00:07:18.780 So this is important, this is really useful in Python 159 00:07:18.780 --> 00:07:21.240 because what we've done is that we have 160 00:07:21.240 --> 00:07:24.480 evaluated this pattern against the price. 161 00:07:24.480 --> 00:07:26.513 That's what RE dot search does. 162 00:07:27.730 --> 00:07:32.550 But then it has allowed us to extract particular parts 163 00:07:32.550 --> 00:07:37.350 of that by using the brackets into group one. 164 00:07:37.350 --> 00:07:41.410 So group zero is the entire match. 165 00:07:41.410 --> 00:07:44.223 Group one is the first thing in brackets. 166 00:07:45.270 --> 00:07:47.350 So why is this useful? 167 00:07:47.350 --> 00:07:49.970 It's useful because now instead of 189.50 168 00:07:51.270 --> 00:07:55.120 we can say, for example, zero to nine 169 00:07:58.050 --> 00:07:58.983 dot plus. 170 00:08:00.360 --> 00:08:01.780 We can put a backslash in this dot 171 00:08:01.780 --> 00:08:06.740 since it's meant to be not anything but rather the dot. 172 00:08:06.740 --> 00:08:07.680 Zero to nine 173 00:08:09.190 --> 00:08:10.103 dot plus. 174 00:08:12.520 --> 00:08:15.013 Sorry, just the plus, not the dot plus, my bad. 175 00:08:16.320 --> 00:08:18.050 So what we've got now is 176 00:08:19.260 --> 00:08:21.610 any of these characters zero to nine 177 00:08:21.610 --> 00:08:23.970 and any amount of them 178 00:08:23.970 --> 00:08:28.090 followed by the period which separates 179 00:08:28.090 --> 00:08:29.690 numbers from their decibel place 180 00:08:30.620 --> 00:08:34.840 followed by zero to nine any number of characters. 181 00:08:34.840 --> 00:08:39.460 So this could potentially be for example, this. 182 00:08:39.460 --> 00:08:40.970 Right, any number of characters, 183 00:08:40.970 --> 00:08:42.220 any number of numbers 184 00:08:42.220 --> 00:08:44.370 followed by any number of numbers with a full stop 185 00:08:44.370 --> 00:08:45.463 in the middle. 186 00:08:45.463 --> 00:08:46.913 So this can match any number. 187 00:08:47.830 --> 00:08:51.400 The great thing is now whatever 188 00:08:53.100 --> 00:08:54.980 this price thing here is, 189 00:08:54.980 --> 00:08:58.870 for example, let's say, we change it to $18000, 190 00:08:58.870 --> 00:09:00.100 we're going to match it here 191 00:09:00.100 --> 00:09:01.080 and when we run 192 00:09:02.240 --> 00:09:05.120 you see that what we have extracted 193 00:09:05.120 --> 00:09:07.150 is the number. 194 00:09:07.150 --> 00:09:09.350 So why is this great? 195 00:09:09.350 --> 00:09:12.050 It's great because we can say the price 196 00:09:12.050 --> 00:09:17.050 or price number is a float of matches dot group one. 197 00:09:17.180 --> 00:09:18.570 So let's run that. 198 00:09:18.570 --> 00:09:21.093 Print price number. 199 00:09:23.560 --> 00:09:26.530 Notice how now we get a third thing printed out here. 200 00:09:26.530 --> 00:09:29.420 18649.5. 201 00:09:29.420 --> 00:09:30.690 So essentially what we've done is 202 00:09:30.690 --> 00:09:34.670 we've turned this incomprehensible piece of text 203 00:09:35.580 --> 00:09:40.343 and we have extracted a Python number from it. 204 00:09:42.090 --> 00:09:46.150 Just imagine you were extracting some data from a website, 205 00:09:46.150 --> 00:09:48.550 now we're gonna be doing that very soon 206 00:09:48.550 --> 00:09:50.850 and you want to extract the price of something 207 00:09:51.830 --> 00:09:55.810 you need to be able to get it out of the text. 208 00:09:55.810 --> 00:09:58.260 And really the only way to do that reliably 209 00:09:58.260 --> 00:10:00.670 is by using regular expressions. 210 00:10:00.670 --> 00:10:05.250 Okay, what about if we do like a comma after the 211 00:10:05.250 --> 00:10:08.033 thousands, like a lot of countries do? 212 00:10:09.140 --> 00:10:10.510 Well, it's pretty simple. 213 00:10:10.510 --> 00:10:12.020 Zero to nine 214 00:10:12.020 --> 00:10:14.960 and you include a comma in there. 215 00:10:14.960 --> 00:10:18.030 So now this is going to match any number of characters 216 00:10:18.030 --> 00:10:20.600 or digits or a comma 217 00:10:22.000 --> 00:10:22.840 any number of times. 218 00:10:22.840 --> 00:10:26.340 So any character from zero to nine or the comma, 219 00:10:26.340 --> 00:10:27.403 any number of times. 220 00:10:28.530 --> 00:10:31.220 I actually don't think we need the backslash here 221 00:10:31.220 --> 00:10:32.700 because I don't think the comma is anything 222 00:10:32.700 --> 00:10:36.270 in Regular Expressions, so it doesn't need a backslash. 223 00:10:36.270 --> 00:10:37.750 Okay, so now we run it again 224 00:10:38.850 --> 00:10:43.850 now the first group here, group one was this 18,649.50 225 00:10:46.460 --> 00:10:48.640 but then we got an error because we cannot convert 226 00:10:48.640 --> 00:10:51.850 18000 with a common in there to a float. 227 00:10:51.850 --> 00:10:54.480 Because Python doesn't know that the comma means. 228 00:10:54.480 --> 00:10:57.170 So what we have to do is, of course, say price 229 00:10:57.170 --> 00:11:00.250 without comma is 230 00:11:00.250 --> 00:11:03.420 and this matches dot group one 231 00:11:04.270 --> 00:11:06.940 then this is a string, remind you. 232 00:11:06.940 --> 00:11:08.730 We're gonna call a method of the string 233 00:11:08.730 --> 00:11:12.210 that is a very useful that is the dot replace method. 234 00:11:12.210 --> 00:11:14.750 And what we're gonna do is we are going to replace 235 00:11:14.750 --> 00:11:17.500 the comma by an empty string. 236 00:11:17.500 --> 00:11:20.200 That's essentially, so now remove the comma. 237 00:11:20.200 --> 00:11:23.710 And now we can turn the float off price without comma. 238 00:11:23.710 --> 00:11:24.773 Let's run this again. 239 00:11:26.250 --> 00:11:28.510 And notice how now we get our float back here. 240 00:11:28.510 --> 00:11:31.700 For Python float we can add it, subtract it, 241 00:11:31.700 --> 00:11:33.870 multiply it, you know anything you want 242 00:11:33.870 --> 00:11:36.420 because now we have the actual price as a number, 243 00:11:36.420 --> 00:11:38.070 not as a string. 244 00:11:38.070 --> 00:11:41.180 So potentially let's say you were getting some data 245 00:11:41.180 --> 00:11:43.800 from Craigslist or something like that, 246 00:11:43.800 --> 00:11:46.580 you could look at the websites with Python, 247 00:11:46.580 --> 00:11:48.820 you could download them, you could load the price, 248 00:11:48.820 --> 00:11:50.670 you could convert it to a float 249 00:11:50.670 --> 00:11:54.060 and then you could compare it against, say your budget 250 00:11:54.060 --> 00:11:56.320 or the hourly rate that you want for your job 251 00:11:56.320 --> 00:11:59.010 or whatever it is that you're searching for in Craigslist. 252 00:11:59.010 --> 00:12:01.960 Craigslist, by the way if you are not in the United States 253 00:12:01.960 --> 00:12:05.613 is a classified ads website like Gumtree in the UK. 254 00:12:07.100 --> 00:12:09.040 So it's pretty useful to be able to extract 255 00:12:09.040 --> 00:12:11.120 meaningful information from text. 256 00:12:11.120 --> 00:12:13.903 That's what Regular Expressions do best. 257 00:12:15.410 --> 00:12:18.960 So again, we've looked at a few examples of how you can use 258 00:12:18.960 --> 00:12:22.103 the RE Module to search through regular expressions. 259 00:12:23.330 --> 00:12:24.580 As we do more, 260 00:12:24.580 --> 00:12:26.900 and as we require the regular expressions module 261 00:12:26.900 --> 00:12:28.280 we're gonna learn more about it. 262 00:12:28.280 --> 00:12:30.780 I don't wanna delve too deep into it 263 00:12:30.780 --> 00:12:32.750 without a use case because I think that's 264 00:12:32.750 --> 00:12:34.320 gonna just be a bit confusing 265 00:12:34.320 --> 00:12:35.580 and a bit pointless. 266 00:12:35.580 --> 00:12:37.810 So let's wait until we have a better use case 267 00:12:37.810 --> 00:12:39.320 but in the meantime I would recommend 268 00:12:39.320 --> 00:12:42.223 that you take the time to look through the 269 00:12:42.223 --> 00:12:43.589 Regexer website if you want 270 00:12:43.589 --> 00:12:46.820 and the official Python documentation for the RE Module 271 00:12:46.820 --> 00:12:48.090 as it's quite good. 272 00:12:48.090 --> 00:12:50.680 And the code that we've looked at in this section, 273 00:12:50.680 --> 00:12:51.800 everything we've written 274 00:12:51.800 --> 00:12:54.050 as well as a couple of links to the other sites 275 00:12:54.050 --> 00:12:55.476 are gonna be included in 276 00:12:55.476 --> 00:12:58.340 the resources section of this lecture 277 00:12:58.340 --> 00:13:01.220 so feel free to have a look if you're interested. 278 00:13:01.220 --> 00:13:02.470 That's it for this video now 279 00:13:02.470 --> 00:13:04.457 and I'll see you on the next one.