so in the last video we started work
with the pattern and the matcher classes
and we saw some results when the entire
string or character sequence matches the
pattern that's good we need to do that
in some situations but how can we find
out the occurrences there are and where
they actually occur
so for that we're going to use the find
the start and the end methods so lets
actually have a look at that
so we can try looking for
occurrences in a string by
creating a loop so...
...
...as you can
probably guess the find method looks for
occurrences of a pattern used to create
the matcher when it finds one the
internal state of the matcher instance are
updated and we can then call the start
and end methods you can see me doing that
on line 73 so the start method returns
the index of the first character
in the match and the end method
returns the index of the character that
occurs right after the last character in
the match so we have two occurrences of
h2 in our pattern you can see above
and so we should expect to see two
occurrences print to the console so lets
actually just run this and see what
happens now obviously something didn't
work so all we are seeing is the true which
is the result of the code on line 68
printing out matcher out . matches so the
loop in other words didn't actually find
anything
well there's a reason for that that's
because we can only use a matcher once
so matchers have an internal state it's
updated whenever we use it if you recall
I said that when we talked about find so
the matches updated and then we can use
the start and end methods
but when we call the matches method so that
we could print true false to the console
the code on line 68 the internal state
of the matcher was updated in such a way
that we have to reset the matcher before
we can use it again so we'll do that by
using the reset method so basically the
line before i'm going to do....
...
let's go run that alright so getting
better this time because we've now got
some output we got the occurrence one
and colon 0 to 179 that's good in the
sense that it's giving us something but
it's obviously not what we expected to
see so what's actually going on now well
the regular expression we used to
determine if h2 occurs anywhere in
the text isn't specific enough when it
comes to pinpointing how many
occurrences there are and also where
they're so we have to use a broad
expression with the matches method
because the entire text have to match
now because we used such a broad
expression
there's only one occurrence of a match
which in these cases of the entire
string and the start and end indices are for
the entire string but now we're looking
to find the occurrences within a string
so we don't need the entire string to
match we only care about matching parts
of the text so that we can go back to
using h2 as the regular expression
let's go back and change the pattern
on line 65 going to change that to just
...so it was
actually a standard HTML tag so now if
we run this we can see that we've now
got the right results we've got two
occurrences and that's obviously the
code here
h2 and h2 keep in mind that the index
return by the end method is actually the
index for the first character that comes
after a match now this obviously works
what we've done but when you're using a
matcher to find multiple occurrences of
a pattern theirs actually an easy way to do
it now we can actually use what's called
a group so use a group in a regular
expression we use parentheses so in this
case we're going to use the regular
expression as we've got on line 65 we're going
to surround that in parentheses so we're
going to come up here and change that
...
...and keep in mind that the
parentheses aren't part of the pattern that
has to be matched when working with
groups we access them using the group
method in the matcher class so one version
of the method takes an int parameter
which is the group number this is for
the group method in the matcher class now
when working with groups the entire
character sequences is group 0 so when we
want to access a specific group we
actually start counting from the number
one so let's actually look at example
we're going to use groups to find
occurrences of h2 so what I've done is I
started making a change to line 65 but
i'm going to just undo that and what
I'll do is instead I'm going to come
down here and we're going to create
another string...
...
..
...
because we've done that we know that we needed
to do a reset if we want to reuse a matcher so...
...
...so here we've updated
the regular expression that we're going
we're going to compile which on line 78
parentheses now and note that the loop
is now different to what it was before
using the previous matcher . find for each
occurrence we call the group method with
a value one and we have more than one
occurrence but we only have one group in
our regular expression so groups 0 is
the entire character sequence so Group
one is our h2 in parenthesis group
which is more or less that's defined on line 78
so let's run this and see what happens
you can see we've got a false there for
the match then we're got our two
occurrences showing not very exciting
but we know that there's two occurrences
of h2 but so what would be nice to be
able to extract every h2 from the
text in its entirety which would include
the opening tag the text and the closing
tag so this is actually very easy to do
all we have to do is change our regular
expression to match the opening tag h2
text and then the closing tag so let's
actually go ahead try changing something
to see how that would work and come back
up to our group pattern h2 group pattern and we are
going to put a . after the parentheses
and star then start the tag / h 2 with
closing h2 tag like so and it has to
have an ending parentheses then we get
rid of that first closing parenthesis so
surrounding the entire regular
expression in one parentheses there
because that's our group so basically
what we're saying here is that we want
the opening tag the closing tag and
everything in between
if you remember that the dot means any
character and then the star quantify
mean zero or more so . star in this case
we'll match one or more of any character
so if we actually run that
it's starting to look like we want but
clearly it's not exactly what we want
because what happened is the regular
expression has grabbed everything from
the opening tag of the first h2 to the
closing tag for the second h2 so the
question would be does that matcher
regular expression
well actually yes it does after all the
strings said so we got start with h2 and
ends with the / h2 and has text in
the middle but it's just obviously not
what we were expecting
and the reason this is happening is due
to a concept that i haven't discussed
yet greedy quantifiers vs reluctant or
lazy quantifies so these star quantifier
is what's called a greedy quantifier so
it'll grab as much text as it can
so when it's doing its matching it
doesn't actually stopped at the h2
the closing h2 tag it actually keeps
going to see if more of the character
sequence can result in a match so in our
example grabbing more text can still
result in a match as we've seen here now
we want to use a lazy quantifier one
that does the minimum amount of work so
once it finds a match it doesn't keep
looking to see if it can include more
characters in the match so it may still
find more matches and later parts of the
character sequence but each match will
contain the minimum number of characters
required for match know the ? quantifier
means one or zero occurrences is a lazy
quantifier so we can use it to turn a
greedy quantifier into a lazy quantifier
by actually adding it after the
quantifier I know that sounds confusing
but we'll see an example of that now
since it wants
no or only one occurrence the moment
the greedy quantifier that it's modifying
finds a match it will stop grabbing
text that's obviously what we want to do
here so that we can actually separate
our h2 occurrences and the reason
it will stop at that point is the
question mark quantify will be satisfied
that point so let's give that a try
so all we really need to do is come up to
our pattern on line 78 h2 group patterns and
put a question mark straight after the
start quantifier and again by doing by
or adding the question mark after the star
return the star quantifier into a lazy
quantifier
so now that we run when we run this we
should see we've got separate
occurrences of h2 and h2 tags and you
can see we've now got two versions we
got a subheading h2 tags and the text
in between and likewise for the
summaries as well so now you've seen how we
could use matches to parse HTML code and
hopefully you now thinking that's a
little bit more exciting than what we've
done so far
one quick note if we weren't interested
in empty h2 tags and that are tags with no
texts between the opening and closing
tag would use the regular expression h2
. + ? and then the closing h2 tag
instead so the plus as supposed to the
star wants one or more occurrences
whereas the star except zero or more
occurrences and we can also use multiple
groups for example with we just one of
the texts between the tags are not
actually the tags themselves we could
change that up a little bit and so here
we're going to add a multiple or
another tag to our text group so let's
write some new code here so....
...
..
...
..
remember we're trying to extract the
texts between the tags and not the tags
themselves so we defined three groups
on line 88 we're looking for a pattern
that matches h2 . + ? and then the
closing h2 tag so we want the h2
part of the match to be assigned to
group 1 remembering the group 0 is the
entire pattern and the . + ? part of the
match that should be assigned to group 2
and then the closing h2 tag part of
the match should be assigned to group 3
so you can see that in the while loop
I've written out the parts that are
assigned to a group 2 and that should
correspond to the h2 text in other
words that text between the h2 tag and
the closing h2 tag
let's run this just to confirm that it
works and you can see we've got some
heading and summary outputting which is
the texts between the opening and
closing h2 tag so that's working nicely
you can actually also use matches to
find and replace parts of a string using
the replace first and replace all methods
now for more information about other
methods in the matchers class and what you
can do with them
I suggest you refer to the match the
documentation and i'll just bring that
to link up for you very useful
information on this link in Oracle's
website lots of good information there
about matchers so we're make sure you
check that out and read up on it to see
what else you can actually do with this
class
alright so I'm going to finish the video
here now in the next one we're going to
talk about using logical operators when
writing regular expressions see you in
the next video