Well
back.
So let me walk you through a few more operations by making use of the split function
as well as some good old indexing. So
I have on my screen two notes, I have an email address and then also a URL.
Now looking at the email address,
what if we wanted to extract the domain specifically?
So
we wanted to extract lab
cyber.com and just get rid of the admin
at.
How are we going to do this? Well, first of all,
let's assign a variable email to be
admin at
Lab
saber.com.
OK?
So
what I'll do right now is I'm going to create
another variable that will now hold what we're looking for.
So I'm going to call that one domain
and now this is where the magic happens. OK?
We want to split
our string, we want to separate admin from lab cyber.com, right? So
in between admin and lab cyber.com is what is the at symbol? So
I'm going to say email
and now split
and now in brackets, what, where are we going to split from?
We're gonna split from the
ATY.
OK.
And now because we're looking for the second element
when you split admin at lala.com from the ad one part will be admin,
the other part will be labs.com, right?
So we want to go with the second element and that's going to be 11 will stand for Lab
cyber.com.
And now all we have to do is to simply print
a
domain.
And if I run the program there, it is Lab cyber.com. If I changed 1 to 0, instead
this will not represent the first element which is going to be admin.
So if I run the program again, right now, you can see it has changed to admin.
So
whenever you're trying to split a string into two,
using the split function,
first of all, you want to indicate OK, where do you want to begin the split from?
In this case right now, we said from the add symbol.
So right down at the middle
on the left will be the index value of zero on the right will be the index value of one.
And since we are looking for the domain specifically,
we change 0 to 1,
we run again
and there you go. We have lab
cyber.com.
Now one of the second example in here, the URL, what exactly are we looking for here?
Let's say we wanted to extract the actual file itself.
So we're not interested in the whole HTTPS
lab.com/path/so
all we're looking for is simply the file name. How can we extract file from this?
So
what I'm gonna do very simply is to say URL
equals.
And let me just grab this,
let me just copy that
and I'm going to paste it
and there you go. OK?
So
again,
I'm going to create a new variable that will
now hold the value of what we're looking for.
Let's call this one, the file under the
name
will now be equal to
URL, right? And now dot split
and now check this out
looking at the URL. OK. This full URL.
What do you think?
What symbol do you think we could use to split the string into multiple parts?
Yep, it should be obvious it's going to be the forward slash, right?
Because you can see right there. It's everywhere.
So we're gonna split our string, we're gonna split our raw
using the forward slash.
And now how can we target the file?
Do you remember negative indexing
where we start from the end of our string?
So in this case right now, what are we, what are we looking for?
We're looking for the very first element from the end
and now that will be negative. What negative one?
So it's gonna start from file, it's gonna start from right to the left, right. So now
all we have to do
is to simply print
the file
underscore name
and they should do the trick.
Let's go ahead and run the program and there you go, we have the file name.
So by making use of the split function and also a negative indexing,
we were able to specifically target the file name.
Let me give you one more example.
And here we're going to extract some strings without using the split function.
We're just going to use the index values. So
let me provide the email address again
and I'm going to say admin
at Lab
cyber.com.
And OK, so what if we wanted to extract both the username and the domain?
So we want to extract the username,
which is admin and then the domain which is labs.com.
How do you think we can do this? Well, first of all,
I can say my username is going to be equal to
email.
And now remember how the indexes work when you have your starts and then the end.
So we want to start from the very beginning of our string.
So we're gonna say colon,
we're gonna leave the left side blank because
we're gonna start from the very beginning.
And now let us see
A is going to be uh index number is zero, right?
So A is zero, D is one, M is two, I is three, N is four and of course, the at symbol is five.
So
I'm gonna say over here right now five.
OK?
And then I can say domain
equals
email. And now inside
we want to start from what the sixth index which is gonna be L
call on and then leave
that blank. Because we, we're going all the way to the end of the string
and now this should work. So all we got to do is to say print
and now in here, let's say username.
OK? And then comma username
and then print
brackets colon
uh domain.
OK.
Comma domain.
And there you go, let's run the program and there it is.
Username is Admin Domain is lab
cyber.com. Yay.
But hold up
just a minute
before you pop the champagne.
Let's run another test. OK.
I'm gonna change admin here to, I don't know, let's say Alex, for example,
never on the program.
We do have a
problem. You can see right now that the program has malfunctioned,
it's no longer working properly
instead of username, Alex, we have
Alex at
and instead of lab cyber.com, we have a
cyber.com.
What is going on here?
See
by trying to extract our strings by being very specific
with the index values that we want to target.
The program is going to rely very heavily
on the actual email address,
having the right number of characters, both before and after the at
symbol.
So it's heavily dependent on that, but we don't want that.
We want the program to be flexible enough
to always be able to accurately extract both the username and the domain
regardless of how short or long the actual email address is.
So
what can we do? Well
remember that the add symbol is where all the action is
to the left of the add symbol is our username to its right is the domain.
So we can target specifically
the index value of the at symbol.
And how do we do that
for the username? Email?
Right now
again,
we're gonna leave the first part of the column
blank because we're starting from the very beginning,
right?
And now to target the actual
at symbol, its index value,
I'm gonna say email again and now dot index
and now in brackets
code the
at symbol.
So now I'm saying the username always begin from the
from always start from the very beginning of the string
and then end once you have reached the index value of the
at symbol,
likewise, we can do something very similar with the domain,
we can say domain.
OK, let's start from the beginning of in here email
dot index.
And now what are we targeting again? We're targeting the AD symbol.
However,
we specifically want to target the very first character,
the very first index after the ad symbol.
So what do you think we're gonna do?
We
the good people, we're gonna say plus one
plus one.
So now I said, OK,
start from the index value of add but
then add one to it. So it's gonna go to add and then add one. OK? Now Lab
cyber.com.
So now if I run the program, there you go. It works. Alex
Lab
cyber.com.
If I change Alex
back to admin
and here on the program, you can see it works.
In fact, let's try something completely different. OK.
Let's say uh Jack Daniels
at Super
yahoo.com something weird, right?
Let's run the program and there you go,
Jack Daniels and then the Domain Super yahoo.com.
So this is how to successfully and efficiently extract
your strings without making use of the split function,
we simply used
the index values
and we targeted specifically the index value of the A symbol.
Because remember again,
the at symbol is where all the action is to the left of
the ad symbol is a username to its right is the domain.
So I hope you enjoyed the video. Thank you for watching.
I will see you in the next class.