alright so now it's time to practice
what we've learned about writing regular
expressions now there's often more than
one way to write a regular expression
that will match what we want to match so
the answers that I'm providing in this
lecture may not always be the only
answer what you should do is verify your
answers using string dot matches string
. replace all and or marchers.
matches and I've been through all of
those in the last few videos in this
section
essentially what i'm saying here is if
your answer doesn't match the one that
I provide that doesn't mean that your
wrong that your answer is incorrect
and that's because i can't really
provide every possible solution because
that's usually the case their are multiple
multiple solutions with programming but
especially with regular expressions
there's can be many differences
alright so let's go on now and I start
looking at these challenges
alright so the first challenge challenge
number one and that's to write the
string literal regular expression that
will match the following string on
screen and you want to use the string
dot matches method to verify your answer
so pause the video now and see if you
can figure that out and i'll see you
when you get back alright so hopefully
you are managed to get that solution
sorted out for that challenge so let's
actually go through and type in the
answer and I've created a new project
here regular expressions challenge and
I'm going to be putting all the
challenges in this particular project
alright so the answer will print...
...
...so we're just using the
string we want to match as the regular
expression obviously when we run that we
should get a true indicating that there
is a match and you can see that their is
a match their and I'll just move this down to the
bottom
alright so that's the first challenge
the regular expression matches the
occurrence of I want a bike on to
the next challenge challenge number two
your challenge now is to write a
regular expression that will match I
want a bike and I want a ball so verify
your answer here using the matches
method so pause the video try that out
and i'll see you when you get back
alright so to do that match to actually
match i wanna biki and I want a ball how
do we do that well we do something like
this...
..
...
...will actually run it
first to make sure it works
always important to make sure
it works you get two more trues there we
got three trues on the screen here using
the / w character class and if
you recall that matches characters a
to z in uppercase a to z in lowercase 0 to 9
and the underscore so it will
actually match one character so we
needed to add that with a quantifier so
i use the + quantifier which indicates
that we want to match on one or more
characters before the period notice
that we have to escape the character
class in the string that's why these two
backslashes there and there would be
another solution here another solution
would be to user groups and it's
actually look at that one as well so we
could do something they're like string....
....
...
alternative way of doing it we can just
do the same there we can actually print
this out or just copy and paste this
remove the second one there so lets
actually check to see whether that
regular expression works i'll just
change that here regular expression one
....so let's check that out and
make sure that works as well you can see
that true down the left-hand side so
what I've used there is a group that's
going to match the bike or ball and I've
also made use of the logical or operator
to as you can see there with that pipe
character
alright that's two challenges down let's
move on to the next challenge
challenge number three
alright so in challenge number two we use the
same regular expression twice so use the
matcher . matches method this time to
check for matches instead of string
.matches for the regular expression that
uses a backslash w+ as a hint
you will have to compile a pattern here
so see if you can figure out
that challenge pause the video and I'll
see you when you get back okay so we're
trying to this time use the matcher
dot matches method to check for matches
instead of the string . matches which we
used previously in this second challenge
now when I indicated as a hint here is
that we needed to compile a pattern so
we're going to be doing just that we're
going to use our regular expression
again string...
...
alright so just run that to verify that
does work
so firstly I compiled a regular
expression into a pattern then I created a
matcher with the challenge one string we
used previously i called the matcher.
matches method and then I did the same
thing for the challenge 2 string except
keeping in mind that at that point
we don't have to compile our pattern again
we can get a new matcher instance using
the same pattern object in our
challenged 2 string and as a result we
don't have to reset the matcher here
because we're using different matcher
instances with each challenge string just in
case you're wondering that's my solution
to that challenge anyway
alright so on now to challenge 4
ok so moving on challenge 4
this challenge is now to replace all
occurrences of a blank like a space with
an underscore for the following string
that you can see on the screen and you
want to print out that resulting string
so you can see the string there is
replaced all blanks with underscores
with spaces need to replace those blanks
or spaces with an underscore so
try that out pause the video now and
i'll see you when you get back alright
so how did you go in that one
hopefully managed to solve that
so my solution to that one is we're
going to create a new string...
...
...but the otherway of
doing it is to use the character class
the backslash s character class which
will match all white space this will
actually be a more broader solution that
would work well if you want to replace
all whitespace with underscores and not
just blanks let's do that one as well
over here that's going to be...
...
....
...
let's try that and make sure that it
works and you can see we got the spaces
now in both the situation both cases
they're replaced with underscores so
that obviously worked alright next
challenge challenge number five
this time the challenges is to write a
regular expression that will match the
following string in its entirety and you
want to use the string . matches method to
verify your answer so see if you can
figure that one out pause the video and
again as always I'll see you when you
get back
alright so for this challenge 5 there
actually are a number of ways of doing
this so I'm actually going to show you a few
ways first way would be a system....
....
...
... so in that case our matching
strings that contain one or more of the
letters A through G inclusive now we can
also have written that with a range so
let's do that as well why we are here...
...
...in case you've used a star quantifier
also work i've used the plus quantifying
as you can see there in both cases
both cases now these out the only
regular expressions that would match
there are others so you're answer might
be different so long it actually works
that's fine and you have actually
succeeded notice the matches method
returns true you're essentially correct
so lets actually just run that again
just to confirm that this is working
we've got two trues and it's obviously
working as well
alright next challenge
challenge number six
challenges to write a regular expression
that will only match the challenge
5-string in its entirety
so again they hint there is only matched
the challenge 5-string in its entirety
see if you can figure that one out pause
the video now and i'll see you when you
get back
ok so i how did you go with that one but
here's my answer and actually are a
couple of other ways to solve this but
we'll go through to first way I'm going
to do challenge 5...
...
...so here I'm using
quantifiers to spell out exactly what i
want to see we got three a's followed by
a b followed by 8 c's followed by an e
followed by three fs followed by G and
then also made use of the beginning of
string and end of string boundary matches
because we want to string to match in
its entirety and not just part of the
string and lets actually just run that
now that's returning false because I've
forgot to put an a in their
try again now we actually got a true their so
actually doing a complete
match now we can actually also verifier
verify the answer here by using
replace all so we can do something like
....
...
if we run that we should see the word
replaced as you can see it's been a
complete replacement that's obviously
a good way to verify your answers using
replace all and just to confirm that
does work if I then change challenge 5
and put a z at the start we should
find that we get a false for the
challenge five . matches and the original
strain should now not be replaced
need to actually putting the character
at the start as well didn't copy that over so
its an exact copy of the
previous regular expression so now if i
run that you can see the original string
was now left untouched and that's
because the complete regular expression
wasn't matched because we had a z
character and we have actually specified
that the entire string had to match and
consequently the previous line also
didn't work because again we actually
wanted to do an exact match complete
match of the entire string alright time
for the next challenge
ok challenge number seven
the challenge here is to write a regular
expression is going to match a string
that starts with a series of letters so
the letters must be followed by a period
after the period there must be a series
of digits so the string KJISL.22
would be an example of a string that
would match the string f5.12a
is an example of a match or something
that wouldn't match so use the string that I
got on screen abcd.135 to test your
regular expression so see if you can figure
that one out pause the video now and
i'll see you when you get back alright
so let's see if we can solve this
challenge will start by putting our
challenge string in...
...
only way to write this regular
expression if the matches method returns
true for whatever you've written than
your regular expression is correct and
the one that I've written above we start
by using the beginning of string boundary
matcher the carrot to ensure that
nothing occurs before the pattern we
then look for one more letters using a
range of characters and the plus
quantifier could use the case
sensitive modifier and specified one
range that's one alternative after the
letters we look for a period since the
period character is a character class
that matches all characters i had to
escape it when we met match the literal
dots and that's why there's two
backslashes and a dot and after that we
expect one or more digits using the
backslash d character class followed by
a quantifier plus to match that part of
the string now the star quantifier here
would be wrong because that would match
strings that look like ABC dot the
strings we want to match must have at
least one
digit after the period finally we
include the dollar boundary matcher at
the end of the string to ensure that
nothing occurs after this pattern
let's just run this to make sure
it actually does work and you can see we've
got a true there so it is working
alright so let's finish the video here
now we're continuing with some more
challenges for our regular expressions
section in the next video