1
00:00:01,010 --> 00:00:05,090
All right guys when the last video we started to get into regular expressions we looked at some of these

2
00:00:05,090 --> 00:00:12,230
Medak characters symbols like the carrot to start with the number assigned to end with the dot the asterisk

3
00:00:12,260 --> 00:00:14,900
and the optional question mark character.

4
00:00:14,900 --> 00:00:19,430
So now what I want to do is get into character sets using brackets.

5
00:00:19,430 --> 00:00:25,340
So we're going to go down here and let's say I want to sort of format this nicely for you guys so you

6
00:00:25,340 --> 00:00:27,110
can have it as a reference.

7
00:00:27,470 --> 00:00:28,420
All of this code.

8
00:00:28,430 --> 00:00:33,830
I mean this course has been pretty modular except for the project so you know if you need to go back

9
00:00:33,830 --> 00:00:38,870
and look at something I want to make it so that it's easy for you to reference and find where you need

10
00:00:39,440 --> 00:00:44,480
so brackets are basically character sets.

11
00:00:45,170 --> 00:00:51,560
So let's let's look at this example that we did with the optional A and E in gray.

12
00:00:51,560 --> 00:01:00,560
So what we can do here is we can say re schools are ex-Russian let's make it case insensitive and we'll

13
00:01:00,560 --> 00:01:05,390
say G R and let's just do G R A Y.

14
00:01:05,390 --> 00:01:10,360
Now we know that this we want this to be able to be in a or an e.

15
00:01:10,680 --> 00:01:17,270
OK so what we can do is we can use brackets here and then anything we put in here like a and e means

16
00:01:17,270 --> 00:01:23,060
that whatever this character is has to be whatever one of whatever is in here.

17
00:01:23,060 --> 00:01:23,440
OK.

18
00:01:23,450 --> 00:01:25,710
So it can be an A or an E.

19
00:01:26,090 --> 00:01:35,090
All right so what we're going to do is actually we just put a comment here and I'll say must be in a

20
00:01:35,690 --> 00:01:37,170
or e.

21
00:01:37,790 --> 00:01:46,190
OK so then let's get rid of this question mark here and let's save that and see what we get so you can

22
00:01:46,190 --> 00:01:48,500
see it works at matches.

23
00:01:48,500 --> 00:01:56,000
If I were to put an e here that matches if I were to get rid of that and save that gives us null so

24
00:01:56,360 --> 00:01:59,060
this is a better solution than this.

25
00:01:59,210 --> 00:02:05,250
Even though we can use a or e here in this case we could also put nothing.

26
00:02:05,450 --> 00:02:10,460
But when we use the character set it has to be in a or an e.

27
00:02:10,460 --> 00:02:14,410
If I were to put it let's say a W that's not going to work.

28
00:02:14,580 --> 00:02:15,880
OK.

29
00:02:16,650 --> 00:02:18,400
What is going on here.

30
00:02:18,440 --> 00:02:18,690
OK.

31
00:02:18,710 --> 00:02:21,250
So has to be an E or an AK.

32
00:02:21,470 --> 00:02:21,890
All right.

33
00:02:21,890 --> 00:02:25,830
Now let's let's see.

34
00:02:25,850 --> 00:02:31,820
Let's go ahead and push this down and let's put this let's do a character set at the beginning so what

35
00:02:31,820 --> 00:02:42,350
I'll do is let's put right here some brackets and then let's say this has to be a G or an F and this

36
00:02:42,350 --> 00:02:44,770
can be case sensitive.

37
00:02:44,770 --> 00:02:51,220
If we get rid of that so let's change this to an A and let's save that run it.

38
00:02:51,230 --> 00:02:57,770
So basically this has to be either a G in uppercase G or an upper case f.

39
00:02:57,830 --> 00:03:05,340
So I'll say must be a G or F so that works.

40
00:03:05,390 --> 00:03:08,650
Let's change it to fray upper uppercase.

41
00:03:08,690 --> 00:03:15,580
That works let's change it to x ray upper case doesn't work because it's not a G or an F.

42
00:03:15,710 --> 00:03:18,800
If we do a lower case g doesn't work.

43
00:03:18,800 --> 00:03:23,290
But again if we add the case insensitive flag that will work.

44
00:03:23,420 --> 00:03:23,750
All right.

45
00:03:23,750 --> 00:03:26,000
Hopefully that makes sense.

46
00:03:26,000 --> 00:03:31,590
Now another thing we can do here let's bring this down.

47
00:03:31,760 --> 00:03:36,980
We could put a carrot symbol inside of the brackets here.

48
00:03:36,980 --> 00:03:37,280
All right.

49
00:03:37,280 --> 00:03:50,310
So if we were to do that what this means is that it'll match anything except a G or F..

50
00:03:50,630 --> 00:03:51,500
OK.

51
00:03:51,800 --> 00:03:54,140
So it's basically the opposite of this.

52
00:03:54,140 --> 00:04:01,850
So if I were to say Gray doesn't actually didn't matter if this was lowercase or upper case but if we

53
00:04:01,850 --> 00:04:03,000
say that we're going to.

54
00:04:03,000 --> 00:04:06,610
No because it is just an F.

55
00:04:06,620 --> 00:04:10,120
If I were to do an F We'll also get now not a match.

56
00:04:10,130 --> 00:04:17,090
If I were to do an x we're gonna get it's going to pass because this tells it that it can be anything

57
00:04:17,090 --> 00:04:18,400
except this.

58
00:04:18,410 --> 00:04:22,430
Now don't get confused by this meaning it has to start with G and f..

59
00:04:22,670 --> 00:04:27,500
If that were the case then we would put it on this side.

60
00:04:28,010 --> 00:04:28,340
All right.

61
00:04:28,340 --> 00:04:34,520
So if I were to move that over here outside of the brackets that just means that it has to begin with

62
00:04:34,520 --> 00:04:36,990
a year and a half and you'll see that passes.

63
00:04:37,010 --> 00:04:41,230
So don't get that confused if it's inside that means basically not.

64
00:04:41,240 --> 00:04:44,270
So not a G or an F.

65
00:04:44,720 --> 00:04:45,020
All right.

66
00:04:45,020 --> 00:04:50,270
Now let's say that we want that to be any upper case letter.

67
00:04:50,270 --> 00:04:57,680
So I'm going to remove the case insensitive flag and and here we can do a range so we could do a uppercase

68
00:04:57,770 --> 00:05:00,670
a dash Z.

69
00:05:01,100 --> 00:05:01,490
All right.

70
00:05:01,490 --> 00:05:04,310
So this can match any I'll say any

71
00:05:06,680 --> 00:05:11,980
uppercase letter and we'll save an x ray matches.

72
00:05:12,010 --> 00:05:12,460
OK.

73
00:05:12,470 --> 00:05:16,010
Gray matches frais matches.

74
00:05:16,340 --> 00:05:19,010
If I do a lower case that's not going to match.

75
00:05:19,100 --> 00:05:23,910
OK because these are upper case and there's no case insensitive flag.

76
00:05:24,350 --> 00:05:36,080
If we wanted to do lowercase we could do lowercase a to lowercase c and this will match any lower case

77
00:05:36,080 --> 00:05:39,610
letters so if I save that phrase will pass.

78
00:05:39,650 --> 00:05:44,180
If I put in lowercase x ray that will pass as well.

79
00:05:44,180 --> 00:05:50,480
Now if you want it to be any letter at all whether it's uppercase or lowercase what you could do is

80
00:05:50,480 --> 00:05:57,030
you could say capital A to capital Z and then leave the lower case a to lower case.

81
00:05:57,050 --> 00:06:02,020
So this will match any letter of any case.

82
00:06:02,030 --> 00:06:10,360
So if we go ahead and save that matches if I change this to an uppercase x ray that also matches.

83
00:06:10,550 --> 00:06:12,900
Now we can also do number ranges.

84
00:06:13,070 --> 00:06:25,040
So if I were to do a dash and in here let's say 0 through 9 and then I don't know Ray which doesn't

85
00:06:25,040 --> 00:06:27,950
really make much sense but it's just an example.

86
00:06:27,980 --> 00:06:40,950
So this will match any any digit so if I were to say x ray that's not going to work if I were to say

87
00:06:41,040 --> 00:06:44,140
one day that'll work OK.

88
00:06:44,190 --> 00:06:48,330
So anything between 0 9 we can make this 0 to 3 if we want.

89
00:06:48,330 --> 00:06:53,510
And if I were to put a 4 that's not going to work so you can put whatever range you want but zero to

90
00:06:53,520 --> 00:06:55,770
nine will be all digits.

91
00:06:55,860 --> 00:06:56,350
OK.

92
00:06:56,400 --> 00:07:05,340
If I were to do 10 that that also works because what it's doing is it's looking at from here over.

93
00:07:05,760 --> 00:07:07,220
OK.

94
00:07:07,650 --> 00:07:14,730
Now we could put 0 3 9 0 through 9 again and save and look at the match now.

95
00:07:14,730 --> 00:07:16,970
Now it's 1 0 OK.

96
00:07:17,070 --> 00:07:18,560
OK.

97
00:07:19,010 --> 00:07:25,670
And I didn't mention this but you can do you know as many expressions as you want.

98
00:07:25,720 --> 00:07:30,190
OK so this would be one letter and we could do another eight Izzi and so on.

99
00:07:30,240 --> 00:07:34,160
But that's kind of a that's a long way of doing it.

100
00:07:34,500 --> 00:07:41,090
There's an easier way and that's using quantifiers which are used with by using curly braces.

101
00:07:41,100 --> 00:07:41,370
All right.

102
00:07:41,370 --> 00:07:48,280
So let's say Brace's let's braces.

103
00:07:49,970 --> 00:07:51,650
And these are quantifiers.

104
00:07:51,900 --> 00:07:56,700
So let's go back to our Hello expression souls.

105
00:07:56,790 --> 00:08:12,090
H E L L O and say re equals power case insensitive flag and let's do h e l o n save.

106
00:08:12,090 --> 00:08:13,820
And of course that matches.

107
00:08:13,860 --> 00:08:19,680
Now let's say that we wanted Ray here to specify two L's.

108
00:08:19,770 --> 00:08:28,260
So what we could do is is put in our quantifier using curly braces and then the number two and then

109
00:08:28,350 --> 00:08:28,780
l.

110
00:08:28,800 --> 00:08:32,120
OK so it's going to look at the character after it.

111
00:08:32,190 --> 00:08:40,640
So it's going save that I'm sorry it's the character before it so l should actually go for it and save

112
00:08:40,650 --> 00:08:43,290
and that matches because there's two elves.

113
00:08:43,290 --> 00:08:47,720
Now if I were to put an extra L make it three and save that doesn't match.

114
00:08:47,730 --> 00:08:52,470
If I were to put one l in save that doesn't match it has to be exactly 2.

115
00:08:52,470 --> 00:08:53,820
In this case.

116
00:08:53,970 --> 00:09:00,610
All right so let's put a comment here and let's say must occur.

117
00:09:01,680 --> 00:09:03,780
Exactly.

118
00:09:03,860 --> 00:09:09,950
Am am just is just a variable amount of times.

119
00:09:10,590 --> 00:09:14,860
OK so let's go ahead and bring this down.

120
00:09:14,970 --> 00:09:20,220
And now we can also do ranges so let's say we want to call him a 4.

121
00:09:20,220 --> 00:09:23,890
So what this means is that we can have two to four owls.

122
00:09:24,060 --> 00:09:30,420
So if I go ahead and run this with three L's that works if I do an extra outlets for that works if I

123
00:09:30,420 --> 00:09:33,830
do an extra which is five does not work.

124
00:09:34,240 --> 00:09:36,980
OK if I do one does not work.

125
00:09:37,140 --> 00:09:42,650
So this means that it has to be between this amount and this amount.

126
00:09:42,750 --> 00:09:43,050
All right.

127
00:09:43,050 --> 00:09:50,240
And then another thing we can do using quantifiers is we can do just two comma.

128
00:09:50,400 --> 00:09:53,200
And what this means is it must occur.

129
00:09:55,180 --> 00:09:56,260
At least

130
00:09:59,290 --> 00:10:03,430
at least and times OK so at least two times.

131
00:10:03,450 --> 00:10:10,060
So if I go h l l o that will of course work if I do you know 10 of them.

132
00:10:10,060 --> 00:10:21,170
That also works if I do one that doesn't work because it has to be at least two ells OK so that those

133
00:10:21,170 --> 00:10:22,690
are quantifiers.

134
00:10:23,600 --> 00:10:26,630
And then the last thing I want to show you is parentheses

135
00:10:30,230 --> 00:10:36,260
parentheses and these are use for grouping.

136
00:10:37,130 --> 00:10:46,040
So for instance if I were to say really rules and here let's say we wanted and you can use these with

137
00:10:46,040 --> 00:10:49,760
quantifiers that's that's one of the main reasons to use these.

138
00:10:49,820 --> 00:10:59,600
Let's say that I wanted a number between 0 and 9 followed by X and I wanted that three times.

139
00:10:59,600 --> 00:11:00,410
OK.

140
00:11:00,530 --> 00:11:06,620
Now doing it this way this three is only looking at this x right here so it's only it's going to look

141
00:11:06,620 --> 00:11:08,950
at a number and then x three times.

142
00:11:09,020 --> 00:11:17,120
But I want a number x number x number x three times so that for that to happen I would group this these

143
00:11:17,150 --> 00:11:19,800
together with parentheses.

144
00:11:19,820 --> 00:11:28,540
So now for going on here and I say 3 x 3 x 3 x and say that's going to match.

145
00:11:28,550 --> 00:11:36,140
Now if I do another X that's also going to work because it's just looking at the first three if I wanted

146
00:11:36,140 --> 00:11:42,890
to I could put a money sign up here to make sure that it.

147
00:11:42,930 --> 00:11:46,450
And after the third after three Xs.

148
00:11:46,460 --> 00:11:53,250
So now if I save that works but if I add another 3 x 6 or 2

149
00:11:55,890 --> 00:11:57,760
0 because.

150
00:11:57,770 --> 00:11:59,510
Yeah because now it's just looking at the end.

151
00:11:59,510 --> 00:12:02,110
So we'll have to do beginning.

152
00:12:02,110 --> 00:12:06,680
So we'll put a carrot here and see that and that does not work.

153
00:12:06,680 --> 00:12:10,780
So now it has to be exactly 3 x 3 times.

154
00:12:11,110 --> 00:12:12,240
OK.

155
00:12:12,710 --> 00:12:16,960
So you'll you use the carrot and the money sign quite a bit.

156
00:12:17,060 --> 00:12:20,370
When you're using regular expressions.

157
00:12:20,390 --> 00:12:20,660
All right.

158
00:12:20,660 --> 00:12:24,890
Now there's a whole another part of regular expressions that I want to go over real quick and that's

159
00:12:24,890 --> 00:12:27,250
shorthand character classes.

160
00:12:27,560 --> 00:12:36,110
If we want to search for a letter or I'm sorry a word character or a digit or a whitespace character

161
00:12:36,110 --> 00:12:38,900
we can do that with the shorthand character class.

162
00:12:38,900 --> 00:12:40,900
So that's what we're going to look into next.

