ok so on with our challenges challenge
number 8
now this challenge is to modify the
regular expression from challenge 7 to
use a group so that we can print all the
digits that occur in a string that
contains multiple occurrences of the
pattern so write all the code required
to accomplish this so create a pattern
matcher etc and use the following string
that you can see on screen to test your
code now there are three occurrences of
the pattern we looked at for in
challenge 7 when you run your code
you should see 1357 and 999 printed to
the console
let's see how you go with that one pause the
video and I'll see you when you get back
alright so onto the solution for
challenge eight first we're going to
create the string so challenge eight
...
....
...
...and note in the previous
section I use
their two groups separated by the braced
by the square brackets but you can do it
all in one like what I've done here as well
....
...
so we are now interested in matching parts
of the string rather than the string as
a whole and you'll notice that I have
removed the carrot and dollar boundary
matches from the previous challenges
regular expression now we're obviously
interested in printing the numbers part
of the pattern and so I put the part of
the pattern that matches the digits into
a group by surrounding that part of the
regular expression with parentheses and
I compile our regular expressions into a
pattern and create a matcher using
that string and then looping over the
matches using the find method for each
match we're printing a group one which
will be the digits group remembering
that group 0 is always the entire string
let's just run this and see that it
actually works so you can see we've got
our three occurrences 135 which is from this
group here seven which is from the next
group and the last one 999
on to the next challenge
now challenged 9
let's suppose that we're reading strings
have to match the patterns we used in
challenge seven challenges 7 and 8 from a
file so tabs are used to separate the
matches with one exception the last
match is followed by a new line so that's
your challenge is to solve this and the
revised challenge 8 string is going to
look like you see on the screen now
so basically the challenges to revise
the regular expression accordingly and
extract all the numbers as we did
in challenge 8 see how you go with that one
pause the video now and i'll see you
when you get back alright so how do we
do that one we're going to start by
putting pattern....
...
....
...
....
...
alright so that's a challenge so the
actual regular expression would be....
....
...
..
...
....so basically all we're
really doing here is updating the
regular expression to use the character
class / S which is going to catch
the white space character that occurs
after each occurrence of letters periods
digits so both tab and the new line are
considered white space so I know I type that
all in to make it look like harder than what it
was but it's really quite a simple
solution as you can see so lets actually
run that importantly to check that this
does work and I need to change the while
for some reason I thought I change
that.....ok
that's obviously the one that we're
working on let's try running it and we still got
three matches so obviously that's
working well as which is excellent
alright so let's move on to the next
challenge
hitting double-figures challenge number
10 alright so instead of printing out the
numbers themselves print out this
started and indices so use the same
string we used to challenge 9 here
you want to make these indices inclusive
for example the start index printed for
135 should be five and the end index should
be seven as a hint here you need to
look at the documentation for the matcher
. start and matcher . end methods and
there's more than one version of these
methods as well you can see the link to
the documentation for the matcher class
and their also linked in the video so pause
the video now and try that out and when
you're ready to see my solution come
back I'll see you when you get back
ok so solution for challenge 10 now this
is a tricky challenge for a couple
reasons first of all we have to use the
versions of the start and end methods
that accept a group number because we
interested in the indices of the group
matches not the pattern matches as a
whole second the end method returns the
first index after the match ends to get
the index of the final matching
character we have to subtract one from
the end method return value so lets
actually try typing this in see if we get
to work
i'm going to do is take a copy of
challenge 9 and just change it so
challenge 10 here hasn't changed and the
pattern won't change either and matcher 10 is
going to be equal to...
...
alright so let's try running this first
and you can see we've got the output
there for the first occurrence start
equals 5 and equals 7 the second occurence 14 and is 14
is the start didn't actually put in the word
start there should probably do that
should I just do that and run
again so with start equals 5 and equals 7 start equals 14
and equals 14 and then the third one
start equals 21 and equals 23 so that is
challenge 10
let's now move on to the next challenge
challenge number 11
so suppose we have the following string
containing points on a graph within
curly braces your challenges to extract
what in the curly braces only see if you
can figure that one out pause the video
now and i'll see you when you get back
ok so what we're looking for string....
...
....
...
if you run that again we get the
same results so more or less be a luck
that we happen to use the same
challenge string for both challenges
alright so moving on to finishing challenge 11
so we need a
pattern now....
....
...
....
...
alright so the actual pattern here
is going to be...
...
...so here
what we're saying is we want anything
inside the curly braces to match and
since the opening and closing curly
braces characters are used in regular
expression
with quantifiers I have escaped them when
we want to match the literal characters
that's why there's a backslash backslash
before the left curly brace and the same
for the right curly brace remember to
put the contents of the curly braces
into a group by using parentheses I've
done and I've used plus dot ? to get the
contents of the braces now the plus
matches any character the dot or
period matches any character the plus
says that we expect at least one character
and the question mark turns the plus
quantifier into a lazy quantifier and it
prevented for matching more characters
and we want essentially so will it will
actually limit the match to the content
of the curly braces if we were to remove
the ? quantifier the can only be one match
because the plus quantifier would gobble ip
everything between the first opening
brace and the last closing brace so when
we run this we should get four
occurrences and you can see we've got
4 appearance's showing...
which is the contents for this 4 curly
braces
alright so that worked fine if we know
that our data only contains points within
curly braces but if we want to be more
precise and only match a digit followed
by a comma and space followed by digits we
could do something like this
let's go ahead and change that we're
going to take a copy of this and call it
challenge 11 a
ok so the difference is here let's
actually add some more content to our
challenge 11 a going to
actually add some invalid input as well
so we're going to try....
...
....so we need to change our
regular expression to cope with that so
I'm going to just delete and type in again....
....
...
im using the backslash d character class followed by
the plus quantifier to indicate that we
want more than one one or more digits
followed by a comma followed by space
followed by one or more digits and you
saw that i also modified the test string
by adding xy 11-12 into the mix so
when we run we should see only the
numerics that are part are within those
curly braces
let's just run that and perhaps
confusingly can actually put 2.4 in
twice there's so lets put it in a different
number in there something that we're not
using 634 and the other thing I'll do is
I'll just to put a bit of a separator
here just run that again so you can see
we've got 0205 1324 it skipped X&Y
because it doesn't match the regular
expression we got to 6 34 11-12 alright
so that's obviously working we didn't
see the XY printed to the console and
just as a final thought if we wanted to
extract only the digits
without comma and space would actually
put each set of digits into its own
group
alright we got three challenges left
let's work on those last three in the
next video