1 00:00:00,620 --> 00:00:02,370 - [Tutor] In this and the next several videos, 2 00:00:02,370 --> 00:00:05,070 we're going to take a look at various other search functions 3 00:00:05,070 --> 00:00:08,060 that are part of the re module. 4 00:00:08,060 --> 00:00:11,870 Now previously, we looked at the full match function, 5 00:00:11,870 --> 00:00:15,740 which only returned a match if in fact the pattern 6 00:00:15,740 --> 00:00:17,460 that we were trying to match, 7 00:00:17,460 --> 00:00:21,290 matched the entire string in which we were searching. 8 00:00:21,290 --> 00:00:23,170 Now we'll look at various other methods 9 00:00:23,170 --> 00:00:25,660 that give us a little bit more flexibility. 10 00:00:25,660 --> 00:00:27,390 And we're going to begin that 11 00:00:27,390 --> 00:00:29,250 with the function called search 12 00:00:29,250 --> 00:00:34,070 which is capable of finding the very first match anywhere 13 00:00:34,070 --> 00:00:37,550 in the string so it doesn't have to match the entire thing. 14 00:00:37,550 --> 00:00:40,890 Now the search function is either going to return none 15 00:00:40,890 --> 00:00:45,160 if a match is not found, which of course evaluates to false. 16 00:00:45,160 --> 00:00:49,330 And if it finds a match, it will return an object 17 00:00:49,330 --> 00:00:51,163 of the data type SRE_Match. 18 00:00:52,760 --> 00:00:55,380 Not that you need to know that data type directly, 19 00:00:55,380 --> 00:00:59,700 but there is an object that contains the match or matches 20 00:00:59,700 --> 00:01:01,410 if we're using some of the other functions, 21 00:01:01,410 --> 00:01:05,480 we'll look at shortly, and that object has methods on it. 22 00:01:05,480 --> 00:01:09,120 So this is the first search that we're going to perform. 23 00:01:09,120 --> 00:01:12,270 In this case, we're looking for the literal string Python 24 00:01:12,270 --> 00:01:15,980 in the string, Python is fun, and clearly it's in there. 25 00:01:15,980 --> 00:01:17,440 So I'll go ahead and execute that 26 00:01:17,440 --> 00:01:19,780 and just to show you that this is an object, 27 00:01:19,780 --> 00:01:22,280 if I type result. and hit Tab, 28 00:01:22,280 --> 00:01:25,450 you can see there's a bunch of different methods inside 29 00:01:25,450 --> 00:01:29,600 that match object that represents whatever, 30 00:01:29,600 --> 00:01:31,810 sub string was actually matched. 31 00:01:31,810 --> 00:01:33,780 Now, let me go ahead and get rid of that. 32 00:01:33,780 --> 00:01:38,590 Let's put in a little if else expression here. 33 00:01:38,590 --> 00:01:41,660 So as you can see, we're using result.group 34 00:01:41,660 --> 00:01:44,180 if the result exists, 35 00:01:44,180 --> 00:01:47,260 otherwise we're going to show not found. 36 00:01:47,260 --> 00:01:51,620 Now, the result object is going to be again none 37 00:01:51,620 --> 00:01:52,850 if there was no match 38 00:01:52,850 --> 00:01:55,550 or a match object if there was a match. 39 00:01:55,550 --> 00:02:00,550 Any object that is not none is going to evaluate to true, 40 00:02:01,450 --> 00:02:04,320 there are a few exceptions to that empty strings, 41 00:02:04,320 --> 00:02:06,360 empty lists, empty data structures, 42 00:02:06,360 --> 00:02:09,070 in general, will evaluate to false, 43 00:02:09,070 --> 00:02:11,850 but other types of objects will evaluate to true. 44 00:02:11,850 --> 00:02:13,800 So if we have a match object, 45 00:02:13,800 --> 00:02:17,540 then we're going to get the group that matched 46 00:02:17,540 --> 00:02:20,830 and in this case, that group is going to be the string, 47 00:02:20,830 --> 00:02:22,850 the literal string, Python. 48 00:02:22,850 --> 00:02:25,550 And of course, we could have used a regular expression 49 00:02:25,550 --> 00:02:28,430 and matched some pattern within the string 50 00:02:28,430 --> 00:02:30,870 if we wanted to as well. 51 00:02:30,870 --> 00:02:34,240 Now, of course, if you don't get a match, like I said, 52 00:02:34,240 --> 00:02:36,910 you are going to get none as the return value. 53 00:02:36,910 --> 00:02:40,040 So if I were to search for fun, that is in the string, 54 00:02:40,040 --> 00:02:42,440 but if I add an exclamation point to that, 55 00:02:42,440 --> 00:02:44,320 which is not in the string, 56 00:02:44,320 --> 00:02:46,993 then we can go ahead and once again, 57 00:02:47,880 --> 00:02:51,830 evaluate the snippet that's now showing as number five here 58 00:02:51,830 --> 00:02:53,640 and you see we get not found 59 00:02:53,640 --> 00:02:58,440 because indeed, fun with an exclamation point was not part 60 00:02:58,440 --> 00:02:59,580 of that string. 61 00:02:59,580 --> 00:03:02,520 Now there's another similar method 62 00:03:02,520 --> 00:03:06,250 or function rather to search called match. 63 00:03:06,250 --> 00:03:08,670 The difference between search and match is that, 64 00:03:08,670 --> 00:03:12,580 search will find the first match anywhere in the string 65 00:03:12,580 --> 00:03:15,200 whereas match only looks for a match 66 00:03:15,200 --> 00:03:17,283 at the beginning of a string.