so at the end of the last video I alluded
to the fact that when writing regular
expressions there's a few logical
operators we can use and or and not
and it turns out we've already been
using the and operator and that's
because it's implicit so when we write
regular expressions like for example
i just put this in a comment we write a
regular expression like ABC and actually
means A and B technically and C
so in a sense here and means and
followed by so A is followed by B
followed by C so lets actually take a
look at the or operator so if you recall
a few videos ago actually gave the
example of a match a regular expression
...that
would match the case on Harry whether
it's uppercase h or lower or lowercase H
basically the regular special match both
cases but we can actually use the or
operator to achieve the same results so
we could do something like...
...
...and we could put
or create a regular expression with
the left square bracket capital h and
the pipe character and lowercase H n
square bracket and....
let's say we wanted to replace that with
the word Larry we could run that and
that should actually find or convert
that to Larry see its converted to Larry
because it's replaced the first h so
you can also just take a copy of that
and then just convert the first letter
to an uppercase h just to confirm
that they always working in both
scenarios
and you can see it's working there as
well so basically what we're saying here
is that if the h or characters is a H
capital H or a lowercase H considered a
match again as you saw that converted it
actually worked and match the regular
expression for both harry with a capital
H and harry with a lowercase H now we've
actually already seen how to use not
when working with a set of characters so
for example the regular expression....
...
....so the carrot character here
means not when it's the first character
within square braces now this regular
expression matches all characters except
a B and C so that's one version of not
but there's also another not operator
and that's the ! character but this
character has to be used in what's
called a negative look ahead expression
so let's look at an example of that
we're going to start off assuming we've
got a string called TV test I'm going to
assign that....so what we want to
do is find all ts that aren't followed by
v so our first attempt to the regular
expression might be something like
string...
...
...so that's our first attempt so
we're using a regular expression that
says we want to match occurences of t
that aren't followed by the character v
there should be three occurrences TS TK
and also the final t in string that
isn't followed by anything so we
actually run this and see what it does
so you can see we've got two scenarios
covered here we got occurrence 1 which is
the character 0 to 2 an occurrence 2 which
is the characters 4 to 6 so the first two
occurrences were found but the
third one the final t wasn't and
there's a reason for that and that's
because when we use the carrot in square
brackets regular expression we actually
match a character or we must match a
character in order for the regular
expression to match so we're saying that
the t must be followed by any
character other than v
but in this case there's no character
following the final T and so our carrot
v in brackets doesn't actually
match anything so another way of saying
this is that the left square bracket
carrot expression right square bracket
assumes a character
so in other words it requires a
character to be instead of using the
square bracket version carrot we can
actually use the not operator which is
the ! in a negative look ahead
expression now look ahead doesn't
consume characters it can actually match
nothing so let's go ahead and change our
regular expression as follows
so instead of the word is at the moment
with t left square bracket carrot v and a
right square bracket
we can actually use this negative look
ahead expression so to do that we're
going to take a copy of this on
the original version to duplicate that
line
comment that out and the regular
expression we're going to use will t
still but this time we're going to use
parentheses here parentheses ? then our
negative operator and then v then
right parenthesis so what we're saying
here is we want t that isn't followed by
a v now the question mark inside the
parentheses is part of the look-ahead
syntax and look ahead always start with
a left parenthesis and a ? now the not
operator the ! here doesn't consume a
character so it will match instances of
t at the end of a string so if we run
this now we should actually get three
lines of output from our clearances we
should get the final matching t
as well as a result you can see we've got
the third one showing there as well so
all three occurrences have now been
found now note that when we use a look
ahead the characters in the look-ahead
aren't part of the match and also note that
the indices indicate that our matches are
one character in length and if you
recall that the end method returns the
index after the match
so then no longer matching the t followed
by another character in other words now
once again look aheds don't consume
the characters they match or putting it
another way they don't actually include
the characters they match in the match
text so that's negative look at
expressions but we can also use positive
look aheads using the equal sign rather
than the not operator which was
the exclamation mark so if we wanted to
find all matches of T followed by v
but we didn't want to include the v in
the match would use a regular expression
like so we do regular expression here
would be...
...so that's the
positive look ahead and again that's it
if we want to find all matches of T followed
by v but we didn't want to include the
V in the match
alright so before we leave the subject
of regular expressions let's look at a
couple of real-world examples now
regular expressions are often used to
validate user input now here's an
example i'm going to copy and paste that
has or is a regular expression for you
to validate a US phone number
I'm just gonna put into a comment for
now so you can get a look at it now
let's actually have a look at what this
does now obviously we know that it's it's
validating a US phone number but it
starts out by indicating that the line
has to start with the entire pattern in
other words if anything occurs before
this pattern in the string then it's not
a phone number so next in this version
we're expecting the area code to be
within parentheses so the first
character we expect is the open
parentheses so we have to escape it
that's because as we've seen the
parentheses is a character class used to
indicate a group now in this case we
want to match the parentheses character
literal and when we want to match the
character literal that can also be used
as a character class in a regular
expression we have to escape it in this
way so following the parentheses is a
quantifier indicating that there should
only be one opening parenthesis so
moving on next we expect the area code
which will be three numbers between 0
and 9 inclusive and again we're using a
quantifier after the range of characters
to specify how many numbers we actually
expect for a match so following that we
expect the closing parenthesis then we
want to see a blank before we see the
rest of the phone number and we only
want to see one blank moving on we
expect three numbers from 0 to 9
inclusive followed by dash followed by
four numbers between 0 and 9 inclusive
and finally we expect the string to end
with the entire pattern which means that
if anything follows the phone number the
regular expression won't match so now
that we've seen that
let's actually try this out so going to
type in a few examples here's so....
....now based on that
regular expression that should match the
...
well that should match because we're
using the right format for the phone
number
...
obviously that should match either
because we haven't space after the area
code right parenthesis so let's actually
try this out i'm going to do a system
...
because we're actually matching the
entire string and we'll just paste
shortly the regular expression into
there and i'll just do the same thing
for the other 3....
and just come back up here and we'll
take a copy of our entire regular
expression will just naturally just
paste this in here the same one for all 4 alright
so let's just try running that we can
see phone 1 is false and the reason that
false is of courses that hasn't
parentheses hasn't got a space and
hasn't got a dash between the first part
of the number and the second part phone
two is true so that matches because it
fulfills the regular expression phone
three doesn't match because it hasn't
got parentheses around the area code and
phone 4 doesn't match because it
hasn't got a space after the right
parenthesis just before the start of the
phone number
now there are many ways to write a
regular expression that matches a phone
number for example might not expect
parentheses around the area code in a
real-world application would probably
remove punctuation and white space from
the phone number before processing it
and we might require the country code
but these are details that would depend
on your specific case another example of
a regular expression would be checking
for a visa card so visa card numbers start
visa credit card numbers start with the
4 new accounts have 16 numbers but older
cards have 13 so the regular
expression i'll just copy and paste to
validate a visa number will be like so
now we start by using the beginning of a
beginning of line character class just
as we did for the phone number then we
expect the character 4 that's because
I mentioned that all visa card
numbers start with the four following
the four we expect at least 12 numbers
next comes the optional three characters
that occur in the newer cards which will put
into a group and followed with the
question mark at the end which will
match 0 or more occurrences of the
group which is what we want here now
there will be zero occurrences of the
final three numbers for older card
numbers and one occurrence for new
card numbers
hence the reason we're using the group
finally there's the dollar boundary
matcher which means that if anything
occurs after this pattern then there's
no match
so let's actually try this with a few
sample numbers and what I'm going to do just to save
as bit of time here because I'm just going to copy
this block of code in because the video is
getting quite long
I'll just paste that in and I've got 4
example here you can see the first two
shouldn't match the last one should
match either and the third one is the one
that's the right length should match and
obviously the first one as well was
13 that should match as well so
basically we've got 2 here that should
match the other two visa 2 shouldn't
match because it starts with a
five and visa start with the four and
visa 4 shouldn't match because it's
only four digits so lets actually just
run that and confirm and you can see
visa 1 and Visa three-match as
they should and visa 2 and visa 4
don't for the reasons that I outlined
now note that this is a comprehensivr
check for valid visa card number some 13
and 16 digit strings would match this
regular expression but fail other steps
required to validate a card number
alright so that's it for our whirlwind
whirlwind tour of regular expressions i
hope you've got a lot out of this
so in the next video will actually start
looking at the challenge for this i'll
see you in the next video