1 00:00:01,210 --> 00:00:02,580 - [Instructor] Now that we've introduced 2 00:00:02,580 --> 00:00:04,620 the asterisk quantifier, 3 00:00:04,620 --> 00:00:08,170 which says zero or more occurrences 4 00:00:08,170 --> 00:00:12,380 of whatever regular expression character precedes it, 5 00:00:12,380 --> 00:00:15,400 let's talk about some of the other quantifiers 6 00:00:15,400 --> 00:00:16,950 that are available to us. 7 00:00:16,950 --> 00:00:19,310 So, of course, asterisk is zero or more 8 00:00:19,310 --> 00:00:21,070 but what if you wanted to guarantee 9 00:00:21,070 --> 00:00:25,140 there is at least one of a particular character? 10 00:00:25,140 --> 00:00:29,905 Well, in that case, we can use the plus quantifier. 11 00:00:29,905 --> 00:00:34,740 So, here we have the expression, the regular expression, 12 00:00:34,740 --> 00:00:37,576 that's going to look for a capital first letter 13 00:00:37,576 --> 00:00:42,100 followed by at least one lowercase letter. 14 00:00:42,100 --> 00:00:45,100 So, of course, Wally has a capital first letter 15 00:00:45,100 --> 00:00:47,330 and at least one lowercase letter 16 00:00:47,330 --> 00:00:49,690 and that does indeed match. 17 00:00:49,690 --> 00:00:52,740 On the other hand, if I were to go ahead 18 00:00:52,740 --> 00:00:56,323 and change this to a capital E, 19 00:00:57,880 --> 00:01:00,364 now, that is going to match this piece, 20 00:01:00,364 --> 00:01:04,950 but there is not at least one lowercase letter following it, 21 00:01:04,950 --> 00:01:07,500 so, in that case, we get invalid 22 00:01:07,500 --> 00:01:09,880 because it's not matching the result, 23 00:01:09,880 --> 00:01:11,670 so we did valid and invalid 24 00:01:11,670 --> 00:01:14,280 in this particular demonstration. 25 00:01:14,280 --> 00:01:16,940 Now, there are some other quantifiers, as well, 26 00:01:16,940 --> 00:01:19,467 and we're going to demonstrate those here. 27 00:01:19,467 --> 00:01:22,070 By the way, I should mention before I do that, 28 00:01:22,070 --> 00:01:25,604 the plus quantifier and the asterisk quantifier 29 00:01:25,604 --> 00:01:28,490 are what we call greedy quantifiers, 30 00:01:28,490 --> 00:01:32,090 which means they're going to match as many characters 31 00:01:32,090 --> 00:01:34,604 as they possibly can, by default. 32 00:01:34,604 --> 00:01:38,600 All right, let's talk about a quantifier 33 00:01:38,600 --> 00:01:43,600 that matches zero or one occurrence of a sub-expression 34 00:01:44,240 --> 00:01:46,220 within a regular expression. 35 00:01:46,220 --> 00:01:49,170 So, let me go ahead and copy and paste in the next one. 36 00:01:49,170 --> 00:01:50,550 Actually, you know what, let's go ahead 37 00:01:50,550 --> 00:01:53,440 and percent-clear this so that we can work 38 00:01:53,440 --> 00:01:55,013 from the top of the screen here. 39 00:01:55,013 --> 00:01:58,280 So, this is the question mark quantifier 40 00:01:58,280 --> 00:02:02,280 and it's going to match zero or one occurrence 41 00:02:02,280 --> 00:02:05,330 of the sub-expression that precedes it, 42 00:02:05,330 --> 00:02:08,460 which, in this case, is a literal l. 43 00:02:08,460 --> 00:02:10,240 So, what we're doing here is creating 44 00:02:10,240 --> 00:02:12,610 a regular expression that's capable, 45 00:02:12,610 --> 00:02:15,660 for example, of matching both the US English 46 00:02:15,660 --> 00:02:19,690 and the UK English spellings of the word labeled. 47 00:02:19,690 --> 00:02:23,132 So this will match one or two l's in a row 48 00:02:23,132 --> 00:02:25,890 but it would not match zero l's 49 00:02:25,890 --> 00:02:27,530 and it would not match three l's 50 00:02:27,530 --> 00:02:31,360 which would be incorrectly spelled words in that case. 51 00:02:31,360 --> 00:02:34,093 So, we do indeed get a match there. 52 00:02:35,470 --> 00:02:38,060 On the other hand, actually not on the other hand, 53 00:02:38,060 --> 00:02:39,900 in this case, let's do it again. 54 00:02:39,900 --> 00:02:43,290 So, we used two l's in this one and that matched 55 00:02:43,290 --> 00:02:46,170 but we want to show that it will work with one l as well, 56 00:02:46,170 --> 00:02:48,470 so, again, we still get the match there. 57 00:02:48,470 --> 00:02:51,680 On the other hand now, if I do three l's 58 00:02:51,680 --> 00:02:53,630 in the string that we're searching, 59 00:02:53,630 --> 00:02:56,010 that will not match because this says 60 00:02:56,010 --> 00:02:59,810 I only want zero or one of the character 61 00:02:59,810 --> 00:03:02,090 that precedes the question mark. 62 00:03:02,090 --> 00:03:03,890 So, let's go ahead and execute that, 63 00:03:03,890 --> 00:03:07,179 and indeed, we can see that we get no match. 64 00:03:07,179 --> 00:03:11,462 Now, you can also specify a quantifier 65 00:03:11,462 --> 00:03:14,976 that's for a range of values. 66 00:03:14,976 --> 00:03:18,970 You can do it for at least a specific number of items 67 00:03:18,970 --> 00:03:23,970 or between a starting and ending value as well. 68 00:03:24,250 --> 00:03:26,820 So let's do at least first. 69 00:03:26,820 --> 00:03:30,120 So here we have a regular expression 70 00:03:30,120 --> 00:03:32,530 with a quantifier that says I would like 71 00:03:32,530 --> 00:03:35,200 at least three digits in a row. 72 00:03:35,200 --> 00:03:39,080 So backslash d is the character class for digits 73 00:03:39,080 --> 00:03:42,730 and three comma inside of curly braces 74 00:03:42,730 --> 00:03:46,120 says I want three or more digits in a row. 75 00:03:46,120 --> 00:03:48,580 Now, of course, we have exactly three digits 76 00:03:48,580 --> 00:03:51,320 right here so I'll go ahead and execute that 77 00:03:51,320 --> 00:03:54,040 and we do get match as a result 78 00:03:54,040 --> 00:03:56,470 and let's do it with more digits 79 00:03:56,470 --> 00:03:59,470 so same regular expression, lots more digits, 80 00:03:59,470 --> 00:04:01,090 we still have a match, 81 00:04:01,090 --> 00:04:04,000 but if I do that same thing once again, 82 00:04:04,000 --> 00:04:08,670 and let's say, this time we cut it down to only two digits, 83 00:04:08,670 --> 00:04:13,190 now we get no match because it's looking for at least three. 84 00:04:13,190 --> 00:04:17,340 Now, you can also do a closed range of values. 85 00:04:17,340 --> 00:04:20,590 So, for example, here we are saying we would like 86 00:04:20,590 --> 00:04:25,260 consecutive digits of either three, up to, 87 00:04:25,260 --> 00:04:28,130 of three up to but, and including, 88 00:04:28,130 --> 00:04:30,840 up to and including six total digits in a row. 89 00:04:30,840 --> 00:04:33,710 So, somewhere between three and six digits 90 00:04:33,710 --> 00:04:36,730 will match this particular regular expression. 91 00:04:36,730 --> 00:04:38,960 So, three certainly does match 92 00:04:38,960 --> 00:04:42,850 and we'll do six next to test the other end, 93 00:04:42,850 --> 00:04:45,870 so that matched, but if I go ahead and modify that, 94 00:04:45,870 --> 00:04:48,030 and let's make that a seven, 95 00:04:48,030 --> 00:04:50,290 so that we have seven digits in a row, 96 00:04:50,290 --> 00:04:52,763 now we have no further match.