so I ended the previous video by saying
that except for the most trivial cases so I ended the previous video by saying
most regular expressions will consist of that except for the most trivial cases
the literals boundary matches and most regular expressions will consist of
character classes strung together and the literals boundary matches and
they're usually be quantifies as character classes strung together and
well and we'll be covering those in a they're usually be quantifies as
minute well and we'll be covering those in a
so one example of using the square minute
brackets in a regular expression would so one example of using the square
be to find a word in a string that maybe brackets in a regular expression would
in upper or lowercase now we could be to find a word in a string that maybe
actually upper or lowercase the in upper or lowercase now we could
string first then you use something like actually upper or lowercase the
string . index of let's just say we string first then you use something like
wanted to find the name Harry with the string . index of let's just say we
capital H or Harry with a lowercase h wanted to find the name Harry with the
without upper or lower casing the string capital H or Harry with a lowercase h
so we could do something like this to without upper or lower casing the string
solve that so we could do something like this to
so we're looking for Harry in uppercase solve that
...... so we're looking for Harry in uppercase
..... ......
.... .....
.... ....
so in the case of the string that was ....
searching for being harry with a so in the case of the string that was
uppercase h if we run that no surprises searching for being harry with a
there we get the upper case H but if we uppercase h if we run that no surprises
change that so its a lowercase H and run there we get the upper case H but if we
that you can see the name is converted change that so its a lowercase H and run
to uppercase so whether it's uppercase that you can see the name is converted
or lowercase it gets converted to to uppercase so whether it's uppercase
uppercase because of what we put in or lowercase it gets converted to
the replacement string so it's obviously uppercase because of what we put in
saving us from actually having to the replacement string so it's obviously
upper or lowercase the action string saving us from actually having to
first upper or lowercase the action string
alright so next what if we wanted to first
replace every letter in the alphanumeric alright so next what if we wanted to
string except for an e or j because the replace every letter in the alphanumeric
alphanumeric strings off-screen what string except for an e or j because the
I'm going to do is to take a copy of alphanumeric strings off-screen what
that paste it down here so we can still see it i I'm going to do is to take a copy of
will call this one the new alphanumeric that paste it down here so we can still see it i
string from this point forward will call this one the new alphanumeric
so what we want to replace every string from this point forward
letter in that string except the E and so what we want to replace every
the J so would we have to write a long letter in that string except the E and
regular expression that contains every the J so would we have to write a long
letter but those two regular expression that contains every
fortunately not we don't need to do letter but those two
that we can actually simplify that by fortunately not we don't need to do
the following that we can actually simplify that by
regular expression so we can do.... the following
..... regular expression so we can do....
.... .....
.... ....
.....so if we run that ....
you can see that the new alphanumeric .....so if we run that
string has been replaced with X's other you can see that the new alphanumeric
than that the occurrence of the E or string has been replaced with X's other
the J now when we use the carrot than that the occurrence of the E or
as the first character inside square the J now when we use the carrot
brackets it's actually a character class as the first character inside square
not a boundary matcher so that might have brackets it's actually a character class
been causing a bit of confusion given not a boundary matcher so that might have
that we've use the carrot as a boundary been causing a bit of confusion given
matcher in the previous video so in this that we've use the carrot as a boundary
particular case because we're using it matcher in the previous video so in this
inside square brackets it negates the particular case because we're using it
pattern that follows it so instead of inside square brackets it negates the
matching all occurrences of E&J the pattern that follows it so instead of
regular expression left square matching all occurrences of E&J the
carrot ej right square bracket regular expression left square
will match all occurrences that are not carrot ej right square bracket
e and j and of course when we ran it you will match all occurrences that are not
saw that does that actually work quite e and j and of course when we ran it you
nicely every character except e and j were saw that does that actually work quite
replaced with the X in this case so to nicely every character except e and j were
reiterate here to get this behavior the replaced with the X in this case so to
carrot has to be inside square brackets reiterate here to get this behavior the
when it's not inside square brackets the carrot has to be inside square brackets
carrot is used to denote the beginning when it's not inside square brackets the
of a string or line carrot is used to denote the beginning
alright so let's so let's say we wanted of a string or line
to replace all occurrences of a through alright so let's so let's say we wanted
f & 3 through 8 inclusive so here's one to replace all occurrences of a through
way to do it so.... f & 3 through 8 inclusive so here's one
..... way to do it so....
... .....
...
....so if we run that you see that works .....
and we've got well at least it seems ....so if we run that you see that works
to partially work but notice the d and we've got well at least it seems
and f weren't replaced and just this is to partially work but notice the d
just really to confirm the fact and f weren't replaced and just this is
that if you haven't gathered this just really to confirm the fact
already the regular expressions are case that if you haven't gathered this
sensitive so in this case the D and the already the regular expressions are case
f weren't replace because they're uppercase sensitive so in this case the D and the
letters but here is a actually shorter way to f weren't replace because they're uppercase
solve what we're trying to achieve this letters but here is a actually shorter way to
we can do.... solve what we're trying to achieve this
.... we can do....
.... ....
..... ....
....you .....
can see we've got exactly the same ....you
result on the screen and the can see we've got exactly the same
dash character in this case that we used result on the screen and the
is specifying range now when we have dash character in this case that we used
multiple ranges as we do here there's no is specifying range now when we have
need to separate them with a comma multiple ranges as we do here there's no
we just actually go on to the next range need to separate them with a comma
as you saw there without adding a space we just actually go on to the next range
between the Ranges now since regular as you saw there without adding a space
expressions are case sensitive if we between the Ranges now since regular
wanted to actually match the capital a expressions are case sensitive if we
through f as well we could write or wanted to actually match the capital a
revise the expression slightly we through f as well we could write or
come in here we can put..... revise the expression slightly we
...and when we ran that come in here we can put.....
you can see this time we've actually got ...and when we ran that
those letters replaced the d and the you can see this time we've actually got
f are now replaced because we specified the those letters replaced the d and the
uppercase versions as well as the f are now replaced because we specified the
lowercase versions with that said there's uppercase versions as well as the
still another way we can do this as well lowercase versions with that said there's
it is actually possible to turn off the still another way we can do this as well
case sensitivity using the following it is actually possible to turn off the
special construct so let's actually have case sensitivity using the following
a look at that.... special construct so let's actually have
.... a look at that....
.... ....
..... ....
..... .....
.....if we do that we're ignoring case .....
now case sensitivity their is no case .....if we do that we're ignoring case
sensitivity and you can see without now case sensitivity their is no case
actually specify the uppercase a-f as we sensitivity and you can see without
did on line 33 we've now got it to actually actually specify the uppercase a-f as we
replace those letters as well did on line 33 we've now got it to actually
and that's because we're using the replace those letters as well
special construct the left parenthesis ? and that's because we're using the
I the cases sensitivity now in our regular special construct the left parenthesis ?
expressions and this works with ascii strings I the cases sensitivity now in our regular
now if the string is unicode expressions and this works with ascii strings
then we actually want to use something now if the string is unicode
like the ? I u would put u on the then we actually want to use something
end if it's actually unicode like the ? I u would put u on the
string like so an interesting bit of end if it's actually unicode
trivia here this actually didn't work string like so an interesting bit of
properly in java 6 and earlier versions trivia here this actually didn't work
the java is actually finally fixed in jdk properly in java 6 and earlier versions
six u2 alright so let's move on the java is actually finally fixed in jdk
let's say we wanted to replace all the six u2 alright so let's move on
numbers in the string now one way of let's say we wanted to replace all the
doing that you do something.... numbers in the string now one way of
.... doing that you do something....
..... ....
.....
....
run that you can see all the digits have .....
been replaced with an x but there's run that you can see all the digits have
actually a shorthand way to do the same been replaced with an x but there's
thing when we want to match any digit we actually a shorthand way to do the same
can actually use the backslash d thing when we want to match any digit we
character class i'm going to copy that can actually use the backslash d
so we can simply make that two character class i'm going to copy that
backslashes and a d and run that and you so we can simply make that two
can see that we've got the same result backslashes and a d and run that and you
and note that i had to escape the / so can see that we've got the same result
in other words had to put a slash a and note that i had to escape the / so
backslash and then put the /d in the in other words had to put a slash a
the actual string to make it a backslash and then put the /d in the
valid string if I don't put the second the actual string to make it a
/ in there we get an error valid string if I don't put the second
illegally escape character string literal / in there we get an error
so I need to put this basically need to illegally escape character string literal
escape / for this to work and there's so I need to put this basically need to
also a shortcut to replace all non escape / for this to work and there's
digits so we can do..... also a shortcut to replace all non
.... digits so we can do.....
..... ....
.....
so you can see there that we replace ....
all non digits as well with X now string so you can see there that we replace
has a trim method that removes white all non digits as well with X now string
spaces from the beginning and end of a has a trim method that removes white
string but let's say that we want all spaces from the beginning and end of a
whitespace removed no matter where it string but let's say that we want all
occurs in the strings so lets actually whitespace removed no matter where it
just declare a new string.... occurs in the strings so lets actually
.... just declare a new string....
.... ....
so if you run that you can see how it prints ....
out there the tab and also a new line so if you run that you can see how it prints
and the spaces as well let's now use a out there the tab and also a new line
regular expression to remove all and the spaces as well let's now use a
whitespace from the string and regular expression to remove all
fortunately there's actually character whitespace from the string and
class to do that so I can do fortunately there's actually character
.... class to do that so I can do
..... ....
.....
.... ....
....so if we ....
actually run that you can see what's ....so if we
happening it's completely removed or actually run that you can see what's
removed all spaces the tab and also the happening it's completely removed or
new line that was added in the first removed all spaces the tab and also the
version of the string of course if you new line that was added in the first
really wanted to remove specific version of the string of course if you
whitespace characters we could specify really wanted to remove specific
those in a regular expression just as whitespace characters we could specify
you would for any other strings so using those in a regular expression just as
backslash t backslash n etc for you would for any other strings so using
example I could do something like backslash t backslash n etc for
.... example I could do something like
.... ....
...you ....
can see the tab character that was ...you
originally shown there has been can see the tab character that was
replaced with an x now if we wanted to originally shown there has been
replace all non-white space characters replaced with an x now if we wanted to
instead we can actually use backslash s replace all non-white space characters
the capital S this time so we can put instead we can actually use backslash s
.... the capital S this time so we can put
.... ....
.....and it's pretty hard to see but ....
that's actually replaced all the non .....and it's pretty hard to see but
whitespace characters instead so all we can that's actually replaced all the non
see on the screen or we can't see it on the whitespace characters instead so all we can
screen is the tab character and the newline see on the screen or we can't see it on the
character but that has actually removed all screen is the tab character and the newline
the non-white space characters instead character but that has actually removed all
another character class is back slash the non-white space characters instead
lowercase w that matches a to z in another character class is back slash
lowercase a to z in uppercase 0 to 9 and lowercase w that matches a to z in
also the underscore so lets actually lowercase a to z in uppercase 0 to 9 and
use that in our new alphanumeric also the underscore so lets actually
string so we can go.... use that in our new alphanumeric
.... string so we can go....
.... ....
....you can see that we've got now a string of ....
x's again we can run that on our white ....you can see that we've got now a string of
space has..... x's again we can run that on our white
.... space has.....
.....you can see here that the ....
whitespace characters are not replaced .....you can see here that the
but everything else was now just like whitespace characters are not replaced
with the backslash s and backslash d but everything else was now just like
using back /w capital W will have the with the backslash s and backslash d
opposite effect so that regular using back /w capital W will have the
this regular expression will match anything but opposite effect so that regular
a to z 0 9 and the underscore so this regular expression will match anything but
that's the / capital W now the a to z 0 9 and the underscore so
backslash b lowercase b boundary that's the / capital W now the
matcher is actually an interesting one backslash b lowercase b boundary
so we actually matches word boundaries matcher is actually an interesting one
so it assumes that words are separated by so we actually matches word boundaries
white space so let's uses that with our so it assumes that words are separated by
has white space string and see what white space so let's uses that with our
we get has white space string and see what
so I'm going to do..... we get
.... so I'm going to do.....
..... ....
..... .....
you can see that each word has been .....
surrounded by the replacement string so you can see that each word has been
we got an x at the start and it surrounded by the replacement string so
also an x at the end of each word so we got an x at the start and it
there are situations in which this would also an x at the end of each word so
be very useful for example we could use there are situations in which this would
it we want to surround each word with be very useful for example we could use
tags of some kind of you know perhaps it we want to surround each word with
your creating some HTML code or needing tags of some kind of you know perhaps
some sort of other tags very quick way to your creating some HTML code or needing
actually replace that put a tag at the some sort of other tags very quick way to
start and the end of every word actually replace that put a tag at the
alright I'm going to finish the video start and the end of every word
here now in the next video we didn't alright I'm going to finish the video
quite get to quantifiers but we'll start here now in the next video we didn't
looking at quantifiers in the next video quite get to quantifiers but we'll start