1 00:00:00,465 --> 00:00:02,580 - [Narrator] In this self check exercise, 2 00:00:02,580 --> 00:00:06,240 we'd like you to take the string 10 plus five 3 00:00:06,240 --> 00:00:08,380 that represents an addition problem, 4 00:00:08,380 --> 00:00:10,032 and use a regular expression 5 00:00:10,032 --> 00:00:14,480 to break that string into three pieces of information 6 00:00:14,480 --> 00:00:16,241 representing the two operands 7 00:00:16,241 --> 00:00:19,200 and the plus operator in between, 8 00:00:19,200 --> 00:00:23,370 and then to display those pieces of information as well. 9 00:00:23,370 --> 00:00:26,330 So go ahead and pause the video, 10 00:00:26,330 --> 00:00:27,550 give it a shot, 11 00:00:27,550 --> 00:00:29,403 and then come back to see the answer. 12 00:00:34,110 --> 00:00:37,730 Okay, first let's go ahead and import the re module, 13 00:00:37,730 --> 00:00:39,500 as we always need to do here, 14 00:00:39,500 --> 00:00:42,710 and this is the regular expression search 15 00:00:42,710 --> 00:00:44,510 that we're performing. 16 00:00:44,510 --> 00:00:47,030 You can see we have a raw string literal 17 00:00:47,030 --> 00:00:49,790 in which we're looking for a subexpression 18 00:00:49,790 --> 00:00:51,370 that we're going to capture, 19 00:00:51,370 --> 00:00:55,610 which consists of one or more digit characters. 20 00:00:55,610 --> 00:00:57,100 Then we have a space 21 00:00:57,100 --> 00:00:59,650 and another sub string that we're going to capture, 22 00:00:59,650 --> 00:01:03,030 and in this case we created a custom character class, 23 00:01:03,030 --> 00:01:05,710 looking for the arithmetic operator characters 24 00:01:05,710 --> 00:01:08,935 minus, plus, times, and divide. 25 00:01:08,935 --> 00:01:10,564 And then we have another space. 26 00:01:10,564 --> 00:01:14,020 And then finally we have another digit, 27 00:01:14,020 --> 00:01:15,300 one or more digits, 28 00:01:15,300 --> 00:01:16,550 that we would like to capture. 29 00:01:16,550 --> 00:01:19,820 So we have three subexpressions that we want to capture, 30 00:01:19,820 --> 00:01:21,551 and we're going to be trying to match 31 00:01:21,551 --> 00:01:25,421 those inside of the string 10 plus five. 32 00:01:25,421 --> 00:01:26,450 Now of course, 33 00:01:26,450 --> 00:01:28,980 we specifically wrote this out 34 00:01:28,980 --> 00:01:31,750 to match up with that string on purpose here, 35 00:01:31,750 --> 00:01:33,860 so let's go ahead an execute that. 36 00:01:33,860 --> 00:01:37,170 Now, let me expand these others snippets here. 37 00:01:37,170 --> 00:01:40,110 So if you want to see all of the matches, 38 00:01:40,110 --> 00:01:43,110 remember you can call the groups method to do that, 39 00:01:43,110 --> 00:01:45,940 so you can see we matched three substrings, 40 00:01:45,940 --> 00:01:46,773 and of course, 41 00:01:46,773 --> 00:01:48,310 once we have those substrings, 42 00:01:48,310 --> 00:01:50,250 we could do things like convert these 43 00:01:50,250 --> 00:01:52,250 into actual numeric values 44 00:01:52,250 --> 00:01:55,890 and use them in a real arithmetic operation 45 00:01:55,890 --> 00:01:57,410 if we wanted to. 46 00:01:57,410 --> 00:01:58,243 Down below here, 47 00:01:58,243 --> 00:01:59,720 we want to just also show you 48 00:01:59,720 --> 00:02:02,620 that you can individually access each of those, 49 00:02:02,620 --> 00:02:04,298 so let's just go ahead and do that, 50 00:02:04,298 --> 00:02:05,800 and you can see again, 51 00:02:05,800 --> 00:02:08,062 starting from index number one, 52 00:02:08,062 --> 00:02:10,630 not index zero, 53 00:02:10,630 --> 00:02:14,273 you can get the values that were captured 54 00:02:14,273 --> 00:02:16,570 by the three subexpressions 55 00:02:16,570 --> 00:02:18,953 in the original, regular expression.