Finance
Mastercard number shape Regex
Mastercard numbers are 16 digits long and start either with 51 through 55 or with the newer 2221 through 2720 range. This pattern spells out those prefixes precisely and then accepts three more groups of four digits, each optionally preceded by a space or dash. It is useful for detecting the card brand in a checkout field, for test fixtures, or for splitting payment data by network during analysis. Remember that it checks the prefix and length only. It does not run the Luhn checksum, so a mistyped digit can still pass, and it says nothing about whether the account is real.
Pattern (JavaScript)
/^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$/Starts at the start of the line/string, then (the character "5", then one of the characters 1-5, then exactly 2 digits OR the text "222", then one of the characters 1-9 OR the text "22", then one of the characters 3-9, then a digit (0-9) OR the character "2", then one of the characters 3-6, then exactly 2 digits OR the text "27", then one of the characters 01, then a digit (0-9) OR the text "2720"), then (one of the characters -, optionally (zero or one time), then exactly 4 digits), exactly 3 times, then the end of the line/string.
Limit: Checks the shape only, not the Luhn checksum or whether the card exists.
Examples
Matches
- ✓ 5555 5555 5555 4444
- ✓ 2223-0031-2200-3222
- ✓ 5105105105105100
- ✓ 2720 0000 0000 0000
Does not match
- ✗ 4111 1111 1111 1111
- ✗ 5655 5555 5555 4444
- ✗ 2721 0000 0000 0000
- ✗ 5555 5555 5555 444
Try it
0 matches (JavaScript engine)
5555 5555 5555 4444
2223-0031-2200-3222
5105105105105100
2720 0000 0000 0000
4111 1111 1111 1111
5655 5555 5555 4444
2721 0000 0000 0000
5555 5555 5555 444Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$/g;
const text = "5555 5555 5555 4444";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$')
text = '5555 5555 5555 4444'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$/';
$text = '5555 5555 5555 4444';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:5[1-5]\\d{2}|222[1-9]|22[3-9]\\d|2[3-6]\\d{2}|27[01]\\d|2720)(?:[ -]?\\d{4}){3}$");
String text = "5555 5555 5555 4444";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$`)
text := "5555 5555 5555 4444"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$");
var text = "5555 5555 5555 4444";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$").unwrap();
let text = "5555 5555 5555 4444";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$/
text = '5555 5555 5555 4444'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why does the pattern include numbers starting with 2?
- In 2017 Mastercard began issuing cards in the 2221 to 2720 range because the 51 to 55 range was filling up. A pattern that only checks for a leading 5 would reject those newer cards.
- Why is the 2-series written as several alternatives?
- Regex has no numeric range operator, so 2221 to 2720 must be broken into digit-by-digit pieces: 2221 to 2229, 2230 to 2299, 2300 to 2699, 2700 to 2719 and finally 2720. Each alternative covers one slice.
- Does this match Maestro cards?
- No. Maestro uses different prefixes and lengths from 12 to 19 digits. Use the generic credit card number pattern if you need to accept Maestro as well.