- [Instructor] Now that we've introduced
the asterisk quantifier,
which says zero or more occurrences
of whatever regular expression character precedes it,
let's talk about some of the other quantifiers
that are available to us.
So, of course, asterisk is zero or more
but what if you wanted to guarantee
there is at least one of a particular character?
Well, in that case, we can use the plus quantifier.
So, here we have the expression, the regular expression,
that's going to look for a capital first letter
followed by at least one lowercase letter.
So, of course, Wally has a capital first letter
and at least one lowercase letter
and that does indeed match.
On the other hand, if I were to go ahead
and change this to a capital E,
now, that is going to match this piece,
but there is not at least one lowercase letter following it,
so, in that case, we get invalid
because it's not matching the result,
so we did valid and invalid
in this particular demonstration.
Now, there are some other quantifiers, as well,
and we're going to demonstrate those here.
By the way, I should mention before I do that,
the plus quantifier and the asterisk quantifier
are what we call greedy quantifiers,
which means they're going to match as many characters
as they possibly can, by default.
All right, let's talk about a quantifier
that matches zero or one occurrence of a sub-expression
within a regular expression.
So, let me go ahead and copy and paste in the next one.
Actually, you know what, let's go ahead
and percent-clear this so that we can work
from the top of the screen here.
So, this is the question mark quantifier
and it's going to match zero or one occurrence
of the sub-expression that precedes it,
which, in this case, is a literal l.
So, what we're doing here is creating
a regular expression that's capable,
for example, of matching both the US English
and the UK English spellings of the word labeled.
So this will match one or two l's in a row
but it would not match zero l's
and it would not match three l's
which would be incorrectly spelled words in that case.
So, we do indeed get a match there.
On the other hand, actually not on the other hand,
in this case, let's do it again.
So, we used two l's in this one and that matched
but we want to show that it will work with one l as well,
so, again, we still get the match there.
On the other hand now, if I do three l's
in the string that we're searching,
that will not match because this says
I only want zero or one of the character
that precedes the question mark.
So, let's go ahead and execute that,
and indeed, we can see that we get no match.
Now, you can also specify a quantifier
that's for a range of values.
You can do it for at least a specific number of items
or between a starting and ending value as well.
So let's do at least first.
So here we have a regular expression
with a quantifier that says I would like
at least three digits in a row.
So backslash d is the character class for digits
and three comma inside of curly braces
says I want three or more digits in a row.
Now, of course, we have exactly three digits
right here so I'll go ahead and execute that
and we do get match as a result
and let's do it with more digits
so same regular expression, lots more digits,
we still have a match,
but if I do that same thing once again,
and let's say, this time we cut it down to only two digits,
now we get no match because it's looking for at least three.
Now, you can also do a closed range of values.
So, for example, here we are saying we would like
consecutive digits of either three, up to,
of three up to but, and including,
up to and including six total digits in a row.
So, somewhere between three and six digits
will match this particular regular expression.
So, three certainly does match
and we'll do six next to test the other end,
so that matched, but if I go ahead and modify that,
and let's make that a seven,
so that we have seven digits in a row,
now we have no further match.