 Hello everybody and welcome back to the regular expression series in Python. In this lesson, we're going to be taking a look at regex use cases. Now, like I said in a previous lesson, sometimes it can be confusing on how you're going to actually use regular expression and what you would actually write. But we're going to take a look at several real use cases that I've used it for in my job before and we'll see how we can do that. As you can see right here, we have this random text and the text says my name is Mr. Neo. My phone number is this phone number. My email is the chosen one at gmail.com. We have that for Neo, Morpheus and Trinity. And what we're going to do is we're going to use regular expression to kind of pinpoint some information that we want to pull out. Let's go ahead and run this and we're going to come right down here. And let's imagine the first thing that we want to do is specify the type of email accounts that these people have. Now, you can imagine there's thousands of these or millions of these, whatever you'd like to do. But our company wants to know are people using Gmail, Yahoo, Apple, AOL, etc. What are they using? And then we can run counts on these things, right? That would be good information to know. So let's go ahead and try to extract this information. Really, what we can first start to look at is just this right here. Yahoo, the Apple and the Gmail. We'll start really simple. Do re, find all. And of course, we're looking at the random underscore text. Now, the pattern that we're going to be looking for is we most likely are going to want to look for an at sign. You'll notice that nowhere else in here, at least in this example, do we have an at sign? So we should come right here and we should look for an at sign. Let's go ahead and take that. Then what we want after it is this text right here. So we're going to say we want lower case or uppercase letters. Sometimes, you know, who knows, it could be uppercase. Usually it's not them. But what we're going to do is a bracket, say a dash C. Now, we know it's all lowercase, so we'll just keep it that. But for example, if you wanted it uppercase, we can also do this. We'll just do a dash Z. And then at the very end, we'll say a plus. Now, why do we need the plus? We need to make sure that we're taking all of it or we're looking for all of it, not just one of them. And let's go ahead and run and just see what we get. So now we have at Gmail, at Yahoo and at Apple. Let's take away that plus really quickly. Now we just get at G, Y and A, which of course we want all the characters after that. Now we're taking all the A through Z. So when we run this, we aren't getting the dot com. And we'll also get that in just a little bit. But let's say we don't want these at signs and I probably wouldn't. I just want the string of the Yahoo and Apple and Gmail. We can do that by using something, a group called a group. We'll use parentheses for this. If we put parentheses around this, we're looking for this pattern, but the only thing that's going to be in our output is what's within our group right here. Let's go ahead and run this. So now we're specifying we only want what's within these parentheses in our output. So now we just have Gmail, Yahoo and Apple. Now if we wanted to get that dot com at the end, and we don't have to, but, you know, while we're here, why not? So we have this A through Z. Now let's actually just get rid of this whole thing. We're going to restart. So we have the at. Now we have to think through how we're going to specify that we want the Gmail.com. Well, we definitely should specify that there's a period right here. We should definitely need to be using that. So in somewhere in here, we'll use a backslash period to specify that there is a period that we're looking for. And we haven't used any character classes really yet. So let's toss one of those in there. And we could use backslash w. That stands for non-alpha numeric. So all of these letters right here would work perfect. So now what we can say is we can put this one in the bracket and we'll say, we're looking for a non-alpha numeric and then a period. And we need to do a plus sign again. Let's try running this. And now again, we get at gmail.com. And if we want to, again, we could wrap it in the parentheses and we'll have gmail.com, Yahoo.com and Apple.com. Again, we don't need the .coms. I was mostly just for just to see if we could. Now let's say we actually want the entire email, which is what I will say. Personally, this is the one that I've used the most. I need to pull out an actual email address. And this is a perfect start. We'll come right down here. We've already got the yahoo.com. And let's take away these right here because when we run this, we want the at gmail.com. We don't want the at not to be in there. So all we need to specify is that there's text before it. It could be a letter. It could be a word. It could be anything. And we should be able to get that using the backslash w as well. So we can almost copy this in a way to take this. Let's put this right here. We don't need this, of course, but we will need a plus because we're looking for more than just one. Then we're saying plus this right here. So let's go ahead and let's try this. And why did I put it right there? I'm going to put it down here. Do you get rid of that? There we go. And this at sign needs to be over here because we're looking for these letters. Then we're saying and the at plus all these so that at gmail.com that we're looking at before. Now let's go ahead and we'll run this. And now we get the actual full email addresses. Now, just to see if this is working, I'm going to put a one right here. Let's run this. And if we run this again, it still works because there are some people who put numbers. And if you just do like a through z here, if you do, you know, a dash z, a dash z. If we did that, it wouldn't work because now it's not going to take that one. And that's what we don't want. So that's why we did it the way we did it instead of using the a dash, the a dash z. We wanted to make sure we encompassed different things that we may not have seen or thought about, you know, when someone was creating these email addresses like a number one in their email address. So we figured out how to get email addresses, which again, I do that. I've done that a lot. The next one we'll take a look at is this one right here, which is phone numbers. These are probably the most, the two that I've used it the most for. I've used it for a lot of other things, but these are the most common, which is why I'm using them as examples. It's coming right down here. We can see both go and let's copy this right down here. Get rid of all of this junk. Now, what we need to do is I want to pull in this entire phone number. I want to make sure that I bring in this entire phone number because we're creating a call list for one of our clients. You know, they said, Hey, you know, we have all these clients. We need their phone numbers, but we have it in this, you know, in the list or whatever, um, we need to pull that information out. So how can we do that? So we'll start with something really simple, which is the backslash d. We'll run that. We get all of our numbers, which is great, but it doesn't really look that good, right? Looking at all of our numbers, all of our numbers, all of our numbers. Um, we don't want to like this. We need it. I would actually looks up here. Now, how can we do that? Well, we can start with looking at a pattern of three numbers. So now this already looks much better. We have one, two, three, four, five, six, seven, eight, nine, zero. So we don't have that last zero and we want to kind of combine these as well. So there's some other things that we need to do to make this really work. What we should do next to specify the next character. The next character is a dash. And then we can literally take this exact same thing. So we're looking for three numbers dash, three numbers, three numbers dash, three numbers, and then we're doing a dash instead of three numbers. We're looking for four numbers. Let's do a dash and now we'll change this to a four. Now we should be able to pull in this entire thing into one value. And that's really what we want. Go ahead and run this. So now we're getting the entire phone number. Now again, in the real world, it's much messier than this. Oftentimes you'll need to clean the data first, get it into the exact format that you want it, let's say this format, and then you'll run something like this. So this is already kind of pre-cleaned or it's pretty for us. So this is how we're able to get both email addresses and phone numbers. That's really all we're trying to look at. But I do want to talk a little bit more about how the data is actually stored. Typically the data is not coming in like this, one giant string. More often than not, it's in something like a list. That's the one that you'll see the most. So let's take something like this and let me copy that again. I don't think I actually got that actually copy from this side. It's not working the other side. I don't know why. So let's create a list. We'll say my underscore list and we'll say this right here. We'll save this. And let's say we want to run some of these other regular expression things that we were running before, except we want to run it on a list. We can definitely do that. It's not crazy difficult to do, but it is going to look a little bit different, of course. And I will say for email in my underscore list. And then we'll do the regular expression. So we can come back up and copy something like this. But now we have to do it on the iterable or the variable or the temp variable that we're looking at and we'll go ahead and run this. If we run this, not going to get an output, of course. We have to do a print from over here. Let's say print and now we get what we were looking at up here, which is gmail.com, yahoo.com and apple.com. Now, this isn't probably how I would actually write it, right? But visually I want to show you, you're looping through each item in your list. You're applying your regular expression to it. But now we're getting all these values in separate lists, which genuinely makes no sense. So and that's because reasons find all. So what we'd probably do is use something like list comprehension. So I could say a domain underscore list is equal to and then I'll create a bracket, which will be our list. And now we're going to apply this in the same way that we did the for loop. And now we're going to say for email in my underscore list. And we'll come right down here and we'll say, we'll print out our domain underscore list. Let's try this. It's not going to work, but let's try it. The issue is, is we're creating a list within several lists. And that's because we're using find all. But because this is a list, we can also use indexing on this. So all we have to do is come right here and we'll put a zero. So when we run this now, you'll notice that it's all within one list. Now, you can apply this same logic, maybe not with the list comprehension, but you can apply the same logic to something like a dictionary or a set or a tuple or really any of those things. And you can apply regular expression to it. Again, typically, you're not going to find data that's just sitting here in this open string, unless you're working with like a large quote or a book or, you know, some website or something like that. Typically, you're going to break it up into you'll have it in list. It'll be broken up into its own individual values. So those are some real use cases that I've used regular expression for many, many, many times in my real job. I wanted to show you kind of walk you through how that actually looks and works. You're not going to probably apply this exact regular expression pattern in your work, but you can adapt it. You just identify what are you looking for in your string? Identify what's there, how you need to write it to specify that pattern. And then you can return those exact values. I really hope this was helpful. Regular expression was really difficult for me when I was first starting out. It was like just crazy complex, but I hope I've kind of walked through it in a way that you can understand it and start using it because genuinely regular expression is really, really powerful and something that I think a lot more people should be utilizing. So with that being said, that is the end of our regular expression series. Thank you guys so much for watching. If you like this series, be sure to like and subscribe below and I'll see you in the next video.