In this video, we'll be using regular expressions to extract the email addresses from this string.
So let's tour the string in a variable.
Let's say this is the text variable execute that cell with control, enter or command.
Enter a then create a new code cell using this button or control G or Command G.
And then here we want to write the regular expression code.
Now, to use irregular expressions, we use the R e-library, which stands for regular.
Then it goes like this.
You create a pattern objects using R e dot compile.
Now remember, I took the boat, a set of special characters that we use for regular expressions, and
we write those characters inside these quotes here in the compiled methods.
So we want to extract only the email addresses, so the final outputs will be a listes containing these
two email addresses.
Now, when you write a regular expression, you first have to understand the pattern of a string pattern
that's the output string has.
So what pattern do we have here with these output strings that we want to extract?
So we have characters than we have at this symbol.
And then we have some more characters which could be column D or other domain extensions.
So we start from here.
We could have any range of letters.
Let's suppose for now, that email's only contains letters.
Let's consider the fact that they could also be dashes in the email or underscores or.
And so on.
We're going to talk about that in just a minute about those characters.
So if there were only characters, you'd see something like square brackets and the square brackets
puts a range from A to Z.
Then if you want a real character, not a special character.
So these are special characters eats like a language by using those square brackets, and that's a dash.
That's you say, give me any characters from A to Z.
And then you use it.
So without square brackets, because it's a literal character, you want to get all those that have
it.
Then let's say again A to Z, then dot.
So a literal a normal string for dots there and then more a to set characters.
Once you have created your pattern.
So this is the regular expression or pattern.
In other words, then you create another variable that's named this matches.
So what string matches we're going to have with output strings?
Then you point to that pattern that you created there and then use the find all methods finds all gets
as arguments.
Can you guess what?
Well, the text variable.
So the text there bring prindle matches.
So in on Jupiter, you don't need to use print matches if the variable is the last one in the cell.
So you can just do matches like that's executed with comments.
Enter or control enter.
And this is the output.
Now we are close, but we are not there yet.
So you see that we didn't manage to extract the complete email addresses.
That is because when we do this.
So this part of the pattern sees that.
Give me one single a letter from A to Z.
We're not specifying how many letters this expressions should match.
So we want to fix that by adding gloss here with class, you see, detect or match any letters from
A to Z one or more times.
So it could be one letter, two letters, three letters and so on.
Let's suppose there are seven letters matched or one two three four five six seven.
Yes, seven letters.
So one or more seven is one or more.
So we get seven letters.
Then we have this at here.
So up to here, if I execute this again, now you see, we're getting closer.
We got all the letters before ET.
But we want to add also here a clause in the second group.
So after the ETS, we also want one or more letters, so let's execute again.
Yes, so example at example, so one or more letters at one or more letters than we have a dot and then
we have one or more letters again.
So plus of the answer also.
And so there we go.
We got two email addresses, so that is an example of a regular expression.
However, we didn't consider all the scenarios here.
So what if we have this underscore here?
Let's execute that cell with the variable is defined, execute the other.
So and you see that we didn't get the entire.
Email address for the first occurrence.
So it started with the underscore ends m p l e m p l e dot example, that's com.
So what the surge is doing here is that it goes to all the characters, one after the other from the
start of a string.
It looks for one or more characters.
So there are one or more characters here, from A to Z.
That's fine.
The company, all methods will start to include the screen, but then it finds out that after the letters,
there's no ET.
So it does not match the this string, then it continues with that.
It does not matter then because there's no ATS offer.
That's and then it goes here.
It does not match those letters from.
So from A to Z, it does not match those because they don't enter with this on the score.
And then it looks at M P, L E and we have this ad.
So it matches.
That's that's also.
And then it matches the other strings.
So A to Z score one or more times, and then it sees that we have a dot, so it matches the dots as
well.
And then it matches the lost boards.
So even if we had something like at blah blah.
So that is not an email address.
So I'm going to execute that cell, execute that as well.
You see that this is not matched because the entire expression should be matched.
And this does not have something before.
It does not have letters before the ATS.
So these letters here are not before that's at.
Therefore, it's a negative outcome.
So back to this under sort of characters, how can we get this email address also?
Well, we have to change the way we think now.
So what we really want to catch here is any character except whitespace for that.
So we need to use this expression.
So this symbol and a space, of course, between these opening and closing square brackets.
So what this says is basically this symbol is a negation.
It sees not space, so match any number of characters except space.
Therefore, the compile methods will look at the string.
It will see we have this year, so no space here, but there's no ads after that.
So it ignores that.
Then there's a space where it ignores that.
And so it goes here.
It sees that we have any character, but space.
So it goes on.
We have ads.
We have also these letters, which we also need to improve by using the same pattern.
So any character, but not the space, the white space.
Now should we use the same for the lost parts?
So for the domain extension, I don't think so because we don't have characters over the letters in,
that's the ports.
So in that case, a dash set is fine.
So I think this is the final code that instructs all the email addresses from this string here.
And you see that we have a list, a python list made or different items, and each item is a string.
What you do with this list of the words is not my business.
So if you could send an email address to those, you could convert them into a pandas dataframe and
save them into a Caesar file.
It's up to your project's needs.
So this was an example to get you started with regular expressions.
Now, I know you're still confused with what's use and when to use words.
So in the next video, I'm going, I'm going to give you a list of the metal characters that you can
use with regular expressions.
So all these are metal characters.
So we have metal characters and we have normal characters.
So this ETS is treated as a normal character, right?
Because it's a literal match and these other ones are metal characters, so eight z and so on.
So I'm going to give you a complete list of these characters in the next video.
See you there!