Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Matching credit card numbers

From: Using Regular Expressions

Video: Matching credit card numbers

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.

Matching credit card numbers

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.

Show transcript

This video is part of

Image for Using Regular Expressions
Using Regular Expressions

59 video lessons · 11682 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 2m 18s
    1. Welcome
      56s
    2. Using the exercise files
      1m 22s
  2. 19m 55s
    1. What are regular expressions?
      3m 20s
    2. The history of regular expressions
      6m 40s
    3. Regular expression engines
      2m 44s
    4. Installing an engine
      4m 5s
    5. Notation conventions and modes
      3m 6s
  3. 21m 23s
    1. Literal characters
      6m 39s
    2. Metacharacters
      2m 1s
    3. The wildcard metacharacter
      4m 31s
    4. Escaping metacharacters
      4m 53s
    5. Other special characters
      3m 19s
  4. 31m 26s
    1. Defining a character set
      5m 49s
    2. Character ranges
      4m 49s
    3. Negative character sets
      4m 53s
    4. Metacharacters inside character sets
      5m 12s
    5. Shorthand character sets
      6m 30s
    6. POSIX bracket expressions
      4m 13s
  5. 36m 38s
    1. Repetition metacharacters
      7m 17s
    2. Quantified repetition
      6m 59s
    3. Greedy expressions
      6m 27s
    4. Lazy expressions
      6m 46s
    5. Using repetition efficiently
      9m 9s
  6. 20m 24s
    1. Grouping metacharacters
      4m 14s
    2. Alternation metacharacter
      4m 54s
    3. Writing logical and efficient alternations
      7m 33s
    4. Repeating and nesting alternations
      3m 43s
  7. 19m 19s
    1. Start and end anchors
      7m 21s
    2. Line breaks and Multiline mode
      4m 41s
    3. Word boundaries
      7m 17s
  8. 23m 33s
    1. Backreferences
      8m 57s
    2. Backreferences to optional expressions
      3m 51s
    3. Finding and replacing using backreferences
      7m 16s
    4. Non-capturing group expressions
      3m 29s
  9. 32m 31s
    1. Positive lookahead assertions
      6m 39s
    2. Double-testing with lookahead assertions
      7m 16s
    3. Negative lookahead assertions
      6m 10s
    4. Lookbehind assertions
      6m 26s
    5. The power of positions
      6m 0s
  10. 13m 13s
    1. About Unicode
      4m 19s
    2. Unicode in regular expressions
      4m 41s
    3. Unicode wildcards and properties
      4m 13s
  11. 1h 55m
    1. How to use this chapter
      5m 38s
    2. Matching names
      6m 33s
    3. Matching postal codes
      8m 54s
    4. Matching email addresses
      5m 0s
    5. Matching URLs
      8m 1s
    6. Matching decimal numbers and currency
      6m 45s
    7. Matching IP addresses
      7m 10s
    8. Matching dates
      7m 49s
    9. Matching times
      8m 59s
    10. Matching HTML tags
      8m 34s
    11. Matching passwords
      6m 49s
    12. Matching credit card numbers
      9m 36s
    13. Finding words near other words
      6m 38s
    14. Formatting with Search and Replace, pt. 1
      7m 22s
    15. Formatting with Search and Replace, pt. 2
      4m 15s
    16. Formatting with Search and Replace, pt. 3
      7m 10s
  12. 47s
    1. Goodbye
      47s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Using Regular Expressions.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.