ok so let's now have a bit of a
discussion about quantifies so in
earlier example we used a regular
expression and what I'm going to do is take
a copy that and paste it down here and
we'll just have work on that and see
how this relates to quantifies so here's
the earlier regular expression that we
used and of course that was looking for
a match at the start of the alphanumeric
string that we just run that you see
the first three letters have been
replaced well in actual fact the first
seven letters ABCDEE have been replaced
with the letters YY and that's just
make another copy of that alphanumeric
string or just get up and scroll up and
take a copy of that so we can sort of
see it more clearly
alright then i'll just change that
alright so that's working now there is
another way to write this expression we
can actually use a quantifier now
quantifiers specify how often an element in
a regular expression can occur in our
example there are three Es clearing in
a row so instead of writing them all out
we can actually use a quantifier as
follows so we can leave the ABCD as well
as the first letter but in here we
can delete the last 2 es and put left
curly bracket right curly bracket and do
it that way so the three and curly
braces indicates the number of the
preceding character that must occur in
order for there to be a match
so in this case we have the way we've
written it we want to see 3 e's and keep
in mind the quantifier always comes
after the character that it applies to
so when we run we should see the same
results we got without using a
quantifier you can see we still got the
yy being replaced so clearly
that works successfully so lets
actually look at doing in a different
way
let's suppose we don't actually care how
many e's their are
we want to match strings that begin with
ABC and a capital D followed by one or
more e's so in that case we use a
different quantifier we use the +
quantifier as follows so we do....
...
....
...
...so run it to confirm it
works and you can see we've still got
yy appearing on there so the good
thing about using this quantifier is we
don't need the curly braces and the
reason for that is we don't we aren't
providing a value so we're just
basically specifying one or more e's
is all we are actually looking for here not
here we're not actually specifying the
actual number so let's actually
suppose we don't care if there are any
e's so the street can begin with ABC
capital D and then it's followed by any
e's we want to match but we also want to
match if it's not followed by any e's
so in other words we want to match
strings that begin with ABCD followed by
zero or more e's so in this case we want
to use the star or the astrix quantifier
so will do...
....
...
....
...run that we're getting the same
result again so just to confirm that
this third one is working let's actually
remove the e's from the beginning of
the string will come up here remove 3
e's if you run this again we can find
that the first two matches have got
ABCDF they basically the same as
the string in other words they haven't
matched anything but the third one
using the star quantifier has actually
replace it
that's because we said the regular
expression was going to be looking for
ABCD and whether it's got an e after
that or not still proceed with the
replacement so you might be wondering
what would this quantifier useful for
well one use might be when you're doing
something like verifying user input and
part of what we've asked for is optional
so in that case you want to look for
that or if it's there if it's not
there and you may still have
some valid input so it really quite useful
for that scenario you can also specify
the minimum and maximum times that a
character can occur here for example let's
say we wanted to match strings that
begin with ABCD followed by two to five
e's and what I'll do there is I will
just take a copy so what I'll do is I'll
just undo that change and put the
three e's back in again so we're back
to where we were before and code on line
51 was still work anyway because
with all the e's
so in this scenario again we want to
match the strings that begin with ABC
capital D followed by 2 to 5 e's so we do
that with....
....
...
....you can see we have got a
match their if we go back and change our string to
only have one e now like so we should
find that won't succeed now because
we're looking for between 2 and 5 e's
you can see the string there has been
left untouched because we've only got
the one e in there and of course we don't
have to match the beginning of a string
we can match parts in the middle or the
end of the string as well as the
beginning so let's actually try
replacing all occurrences of h
followed by any number of i's followed
by at least one j with a y so we
can do something like...
...
what we're trying to do is replace all
occurrences of h followed by any
number of my i's followed by at least
one j and we want to replace all that with
a y so do that we're going....
...
so again we want to see one h hence we use the +
followed by zero or more i's hence
we're using the star followed by j and
when we run this what we should see is the
hhiij part of the string has been
replaced with a single y you can see
that being the case or that is
the case there now there are other
character classes boundary matches and
quantifiers that we can use in regular
expressions and there's actually quite a
complete list at the documentation or of
documentation on oracles websites so
let's just go there and have a quick
look
so you can find quite a bit of detail
there at various patterns and so forth
so I do suggest you check that out and
experiment you know the basics of regular
expressions one thing that you'll notice
with this documentation is that talking
about the pattern class now we've been
using the string . matches and string
. replace all methods to demonstrate
the basics of writing regular
expressions and we haven't used
the pattern class yet but we can also
use this pattern class to manipulate
strings using regular expressions now
some java api always want to work with
patterns rather than a string that
represents a regular expression so we
can use the pattern . compile method to
compile a regular expression into a
pattern now this is often done when we
want to work with methods in the matcher
class
now matches work with classes that
implement the char sequence interface
and that means that we can use matches
with strings stringbuffer stringbuilder
and other classes that implement that
interface generally we use a matcher when
we want to find multiple occurrences of
a pattern or when we want to use the
same pattern
multiple sequences now if we are only going to use
a regular expression once to check one
occurrence in the string then we
probably don't need to use a matcher
alright so let's look at example at an
example we'll go back to our code let's
say we want to find all the
occurences of the h2 tag in a char
sequence of HTML text let's start by
assigning some sample text string so....
....
...
....
...
....
...
....ok got some basic HTML there and in
case you're wondering i'm using a
stringbuilder because we're creating we
creating a long string so assume that
this HTML text is part of a large page
within a HTML body that
perhaps has been now extracted by some
other process
alright so let's start using a matcher
start by using a matcher to find out if
there's any h 2 tags within this
text so the way we do that it would put
....
...
....
...
alright so again what we're doing is
we're using the regular expression h2
as a tag to find occurrences of this
tag which we have assigned to this string called
h2 pattern next we
compile the pattern so that creates a
patent instance that we can use to
create a matcher instance which we
done on the next line
so finally we call the matches method
inside the matchers class to check
whether the pattern matches the regular
expression and once you do have a
pattern you can use that to create as
many matches as you want you might be
thinking that is won't match tags are in
uppercase and you're actually right so
we have two choices to fixes we could
modify the regular expression to also
look for h2 with an uppercase h or when
we can compile the pattern we could pass
the pattern . case_
insensitive constant as the second
parameter so we could do something like
this.....
....
....that's one way to do a
case-insensitive pattern check or using
Unicode you actually leave that in but
you're actually also put that like so
pattern . unicode case there's one here
that's how you actually be searching
unicode strings gonna leave that out
now because there's another alternative
but in our case just for
simplicity we're going to assume that
all our tags in the HTML are in lowercase
alright so lets actually run this and
see what happens
now interestingly enough we get false
and that might not be what you think
should happen because after all we have
clearly got a h 2 their and our text
clearly has got h2 in it there's no
issues with upper or lowercase
so what's going on why isn't this
working well it turns out that the
matches method
wants the match the string as a whole
just like the string . matches method
wants to do so what we need to do is
write a regular expression is going to
match the entire text so we can do this
quite easy with a regular expression
as follows
we're going to just modify our
expression to put ........
....
...so
here we're combining the
. character class with a star quantifier
if you recall the . will match every
character and star means zero or more so
effectively what we're saying here is
that can they can be anything before
anything after the h2 and will actually
have a match if we run this now now that
we're actually testing the whole line we
should find that will return true and
you can see it now returning true and
again that's because the entire string
or character sequence now matches the pattern
alright so that's all well and good but
how can we find out how many occurrences
there are and where they occur
let's talk about doing that in the next
video