Viewers: in countries Watching now:
Learn how to find and manipulate text quickly and easily using regular expressions. Author Kevin Skoglund covers the basic syntax of regular expressions, shows how to create flexible matching patterns, and demonstrates how the regular expression engine parses text to find matches. The course also covers referring back to previous matches with backreferences and creating complex matching patterns with lookaround assertions, and explores the most common applications of regular expressions.
In this movie, we're going to learn to write a regular expression to match credit card number formats. We're only going to be looking at the formats, because we have no way of knowing whether a credit card is actually valid without submitting it to the payment processor. But we can check ahead of time to make sure that the credit card number is in the right format before we send it off to the credit card processor and wait for their reply. For example, if we're expecting to have a 16 digit credit card number, then if we were only given seven digits, we know that's not a valid credit card number, and there's no point in submitting it to the payment processor. We're going to be looking at four major credit cards.
We're going to look at American Express, Visa, MasterCard, and Discover. For each one of these, I've given you a sample credit card, and I've written it twice. The first time is the number without any spaces or delimiters inside it; just numbers in a sequence. The second time I've listed it, it's the same number, but this time with delimiters added in the spots where the credit card company typically adds them. Let's examine the data, so we can start thinking about how we would construct a regular expression. The first thing you'll notice is that Visa, MasterCard, and Discover are all written as 16 digit numbers.
When they have delimiters in between them, they're written in sections of four: four numbers, then a delimiter, four numbers, delimiter, four numbers, delimiter, four numbers. American Express, on the other hand, is different from those three. It only has 15 numbers, and they're formatted as four numbers, then a delimiter, then six numbers, then a delimiter, and then five numbers. And I've numbered each of the segments in this fake credit card number sequentially to help make that clear. The first four digits help us to identify what credit card type it is. So if you have a 3 as the first digit, it's American Express, 4 is Visa, 5 is MasterCard, and 6 is Discover.
It actually goes a little bit further than that, because American Express is typically either 34 or 37. Visa can really be just about anything after the 4. MasterCard is usually 51 all the way up to 55. And Discover typically starts with the exact four digits, 6011. So while most of the credit card numbers could be any digit, 0 through 9, these first four digits are a little bit special, and we'll have to take a little bit more care with them. So let's try and write a regular expression for it. So the first thing we want to do is we want to turn on multi-line anchors, so that we can match whole lines multiple times, and we're going to use our anchors here to make sure that we match exactly what's on one whole line.
And to start out with, let's just do something really simple. Let's just do a digit repeated one or more times. So that matches all of the credit cards that are not delimited. So what about those delimiters now? Let's add in the delimiters as well. We'll put in our character set here, and we'll say that it should be backslash, dash, and I'm also going to put in space, because it is possible that they would just delimit it by putting a space in there. You could add others if you think they're appropriate. I think those are probably the most common ones. So now we've matched all our credit card numbers, nice and simple, but we haven't made sure that they are the correct length.
So as I said, American Express is 15 digits long; Visa, MasterCard, and Discover are all 16 digits long. Well we know how to quantify a repetition. We'll just change this, and we'll put in 15, 16. Now, incidentally, if you were trying to use some other cards, like Diners Club, and Carte Blanche, those are both 14 characters long. So don't assume that it's always going to be 15 or 16. We're really just talking about these four major credit cards. You'd have to look it up for any others. Notice that it no longer matches the ones that have our dividers anymore.
That's because we've got between two and three dividers there, and in that case, the string actually becomes longer. Now, we could just say alright, well it could be as much as 19 long, and that would match it. But it would also match if we had just 19 digits, so that's not a real great solution. I think the better thing here is to start putting those delimiters in the right place. So if we have a delimiter, it must be in the right place. Remember, AmEx handles its delimiters differently from Visa, MasterCard, and Discover. So let's leave it out for now, and let's just focus on Visa, MasterCard, and Discover.
So let's rewrite our regular expression here. I'm just going to change this so that this now is our delimiter, and we know that we need four digits. Then we have our delimiter. I'll erase that. Now, the delimiter may or may not appear, so we'll put a question mark around that, and then right after that, let's just copy this whole thing, and let's paste it in. Again, one last time, but let's take out the delimiter. So there we are: four digits, then a delimiter, four digits, delimiter, four digits, delimiter, four digits, and the delimiter is optional in each of those cases.
So now we've got something that matches Visa, MasterCard, and Discover. Now, there's one minor point here, which is, what if we actually had a number that was like 4001234-1234 1234? That still matches too, even though I did some crazy stuff with my delimiters. If we want to take that out, if we care about it, and want to make sure that they actually do delimit it consistently, then what we need to do is use our backreferences here. We'll capture this, and then we say, alright; if you use a delimiter, then each and every time you use it, I expect it to be the same delimiter again.
Let's put back in our crazy one here, and you see that that no longer matches. Now, you could just stop there, but as I said, those first four digits actually cannot just be any digit at all. There's a little more specificity there, and we can provide that if you want. Be aware, as we become more specific, it does become more brittle. And if it turns out that some of our assumptions about what values can be in those four digits change in the future, we may certainly disallow certain credit cards from working with our regular expression. So let's keep that in mind, but as an exercise, let's go ahead and make it more specific.
So what we want to do is say alright, instead of just being any four characters here, I'm going to be more specific, and I'm going to say that actually this first digit can only be a 4, 5, or a 6. So there we go. So now it requires that that first digit be 4, 5, or 6, and then the rest of the format will apply. I think that's a reasonable improvement. Let's go a little further though. If it's a Discover card, we know it actually ends in 6011. So let's take 6 out of this possibility, let's put the parentheses around it, and let's say either it is going to be 4 or 5 with three digits, or it's going to be exactly 6011.
Now, notice when I did this, it broke the match. It no longer matches. Do you see why? It's because of the captured groups. We're using these backreferences here. We're making a reference to the captured group one, but now this is the first captured group. If we don't want to capture it, we need to turn it into a non-capturing group. So that's a little better. One of the other rules, though, is that MasterCard is numbers starting in 5, and the second digit can be a 1 through 5. 56 is not a possibility, at least currently. So let's add in that possibility as well. We can say, or it's a 5, in which case the next number will be 1 through 5, and then two digits after it. Let's take the 5 out of here, and actually it's no longer a set.
So there we go. Now it's either a 4 with three digits, a 5 followed by 1 through 5 and two digits, or it's 6011. Let's set this aside now, and let's work on the American Express one. And just so it doesn't get in our way, I'm actually going to open up a window here, and I'm going to take this whole regular expression right now, and paste it in here. Let's spread it out a little bit, so we can see it all, and I'm just going to leave that in the background. So now let's get rid of all of this, and let's look at American Express. So for American Express, we can apply the things that we were just working with.
We know that it's going to start with a 3, and we know that the second digit is either going to be a 4, or a 7. Then, after that, there are 13 other digits. So we can just simply enumerate them like that, and we match it, but that doesn't allow for our grouping. Now, if you remember, I told you that the grouping is 4, 6, and 5. So let's make this one 5, and then we'll jump back here and say that there should be a 6 in the middle, and this one needs another two digits after it. Then we can do the same thing that we did over here.
In fact, let's go ahead and grab it. Here is our optional delimiter. So here's a delimiter here, and we're going to use a backreference there, because we learned about that already. So now we've matched our American Express card in both cases. Now let's take the two, and let's put them together. So what we're going to do is just grab this whole regular expression. I'm not going to grab the anchors, because I've already got those. And let's paste it in. Let's say, either it's American Express, or it's going to be our other option here, and let's put that the whole thing inside parentheses, just to keep it separate from our anchors, because we now have that alternation.
So either it's American Express, or it's one of these other three options. Notice that we broke some of these; that some of them are not working. Do you see the reason why? Once again, it's about the capture and the backreferences here. We need to turn this one into a non-capturing group, and that fixed one of them, but it broke the second one. Why did it do that? Well, now this is our first capture, and we're using a backreference to it. This one is now the second one. So we need to put in number 2 there, and a number 2 there.
Now we've got our captures and our backreferences all ordered correctly, and we've got our non-capturing groups all sorted out. So now we're able to match all four of these credit card types, with or without delimiters. Now, I've shown you the general principles behind how you handle this, but I don't want you to think that this is a one size fits all regular expression for credit cards. This will match most cases. However, the burden is going to be on you to go out, and check each of these four formats, and make sure that there haven't been changes to the possible numbers that can be there, especially if you decide to make it really specific.
And it will be up to you to keep up with those changes in the future as well.
There are currently no FAQs about Using Regular Expressions.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.