Pattern: Credit card number

“Credit card number” is one of the patterns that you can select on the Match panel. Use this pattern to match a series of digits that looks like a credit card number from one or more of the world’s major credit card issuers.

This example shows how you can use the “credit card” pattern to look for credit card numbers. You can find this example as “Pattern: credit card number” in the RegexMagic library.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample that has a block of valid card numbers and a block of invalid card numbers for each of the major credit card brands:
    Visa
    4123456789012
    4123456789012345
    
    412345678901
    41234567890123
    412345678901234
    41234567890123456
    
    MasterCard
    5123456789012345
    
    512345678901234
    51234567890123455
    
    Discover
    6011123456789012
    6512345678901234
    
    601112345678901
    60111234567890123
    
    American Express
    341234567890123
    371234567890123
    
    34123456789012
    301234567890123
    3712345678901234
    
    Diner's Club
    30012345678901
    30512345678901
    36012345678901
    36912345678901
    38012345678901
    38912345678901
    
    30612345678901
    37012345678901
    
    JCB
    213112345678901
    180012345678901
    3512345678901234
    
    21311234567890
    18001234567890
    2131123456789012
    1800123456789012
    35123456789012
    
    Random
    123456789012345
    
  3. On the Match panel, click the Add First Field button to add field 1.
  4. In the “pattern to match field” drop-down list, select “credit card number”.



  5. Tick all the checkboxes for the credit card brands.
  6. Set the “field validation mode” to “average”.
  7. On the Regex panel, select “C# (.NET 2.0–7.0)” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    \b(?:4[0-9]{12}(?:[0-9]{3})?|(?:5[1-5][0-9]{2}|2720|27[01][0-9]|2[3-6][0-9]{2}|22[3-9][0-9]|222[1-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35[0-9]{3})[0-9]{11})\b

    Unused options: Case sensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture.

  8. The Samples panel now highlights the valid credit card numbers matched by our regular expression:
    Visa
    4123456789012
    4123456789012345
    
    412345678901
    41234567890123
    412345678901234
    41234567890123456
    
    MasterCard
    5123456789012345
    
    512345678901234
    51234567890123455
    
    Discover
    6011123456789012
    6512345678901234
    
    601112345678901
    60111234567890123
    
    American Express
    341234567890123
    371234567890123
    
    34123456789012
    301234567890123
    3712345678901234
    
    Diner's Club
    30012345678901
    30512345678901
    36012345678901
    36912345678901
    38012345678901
    38912345678901
    
    30612345678901
    37012345678901
    
    JCB
    213112345678901
    180012345678901
    3512345678901234
    
    21311234567890
    18001234567890
    2131123456789012
    1800123456789012
    35123456789012
    
    Random
    123456789012345
    

Reference