Skip to content

Instantly share code, notes, and snippets.

@burrielrush
Last active June 11, 2025 14:52
Show Gist options
  • Save burrielrush/92d8ca93c1aa8e2c68fec354f2716a1e to your computer and use it in GitHub Desktop.
Save burrielrush/92d8ca93c1aa8e2c68fec354f2716a1e to your computer and use it in GitHub Desktop.
This is a Gist breaking down credit card validation Regex

All American Regex

(Credit card # validation regular expression)

The regex \b(?:\d[ -]*?){13,16}\b is a powerful tool used for validating credit card numbers. Credit card numbers typically consist of a sequence of 13 to 16 digits, with optional spaces or dashes in between. This regular expression applies the Luhn algorithm to ensure the validity of credit card numbers by checking for the correct number of digits and the proper formatting. By using this regular expression, developers can easily implement credit card number validation in their applications, helping to enhance security and prevent errors during data entry.

Summary

The regular expression \b(?:\d[ -]*?){13,16}\b is used for validating credit card numbers. It checks for a sequence of 13 to 16 digits, allowing optional spaces or dashes in between. In the explanation, we will explore how this regex applies the Luhn algorithm to ensure the validity of credit card numbers.

\b(?:\d[ -]*?){13,16}\b

Table of Contents

Regex Components

Anchors

In the regular expression \b(?:\d[ -]*?){13,16}\b, the anchors used are \b which represent word boundaries. Word boundaries match the positions where a word character (such as a digit or letter) is followed or preceded by a non-word character (such as a space or puctuation) or the start/end of a string. The \b anchors in the given regular expression ensure that the credit card number is matched as a whole word, preventing partial matches within larger strings. For example, if you have the text "My credit card number is 1234567890123456", the regex \b(?:\d[ -]*?){13,16}\b will only match the credit card number "1234567890123456" and not partial matches like "1234" or "567890".

Quantifiers

In the regular expression \b(?:\d[ -]*?){13,16}\b, the quantifier used is {13,16}.

The {13,16} quantifier specifies a range, indicating that the preceding pattern should match a certain number of times between 13 and 16, inclusive. In this case, it applies to the group (?:\d[ -]*?), which represents a digit followed by optional spaces or dashes. So, the regex will match if there are 13 to 16 occurrences of a digit followed by optional spaces or dashes. This ensures that the credit card number has the appropriate length, allowing for some flexibility in the presence of separators within the number.

Grouping Constructs

The grouping construct used for this expression is (?:...), it is used for grouping without capturing the matched content. In this regex, it is used to group the pattern \d[ -]*?, which represents a digit followed by optional spaces or dashes. The non-capturing group allows us to apply the quantifier {13,16} to the entire group rather than just the preceding element \d. It ensures that the digit followed by optional spaces or dashes is repeated 13 to 16 times, as specified by the quantifier.

Bracket Expressions

The bracket expression used in the regex is [ -], this expression indicates that the regex can match either a space or a hyphen at that position. In the context of the credit card number regex, the bracket expression [ -] allows for optional spaces or dashes between the digits in the credit card number. This means that the credit card number can have spaces or dashes as separators, but they are not required.

Character Classes

\d is the character class used, this character class represents any digit from 0 to 9. It is equivalent to [0-9]. In the regex, \d matches a single digit. The \d character class is used in conjunction with other constructs to match the digits in the credit card number. For example, in the credit card number "1234567890123456", \d would match each individual digit from 1 to 6.

The OR Operator

While the OR operator is a common component of regular expressions, it is not utilized in the specific regular expression \b(?:\d[ -]*?){13,16}\b. The purpose of the regular expression \b(?:\d[ -]*?){13,16}\b is to validate credit card numbers by matching a sequence of 13 to 16 digits with optional spaces or dashes in between. It focuses on ensuring the proper structure and length of credit card numbers, rather than providing alternative patterns.

Flags

In the regular expression \b(?:\d[ -]*?){13,16}\b, there are no flags used. However some common flags included in regex would include: i(ignore case): matches charachters regardless of case sensitivity. g(global): Continues searching for all matches in the input string instead of stopping at the first match. m(multiline): Changes the behavior of ^ and $ anchors to match the start and end of each line within a multi-line input.

Character Escapes

In the regular expression \b(?:\d[ -]*?){13,16}\b, there are no character escapes used. Character escapes in regular expressions are special sequences that allow you to match specific characters with special meanings. They are denoted by a backslash () followed by a character or combination of characters.

Author

This Gist was written by Rush Burriel

burrielrush

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment