Finance
Credit card number (generic shape) Regex
This pattern checks whether a string looks like a payment card number: 13 to 19 digits, with an optional single space or dash after any digit. It suits checkout forms, data-loss scanners and log redaction jobs where you need to spot card-shaped values before they are stored. Because it does not care about the issuing network, it accepts Visa, Mastercard, Amex and many others in one rule. The honest limit is that it only checks the digit count and separators. It does not run the Luhn checksum, does not know which prefixes are real, and cannot tell you whether a card exists or has funds.
Pattern (JavaScript)
/^(?:\d[ -]?){12,18}\d$/Starts at the start of the line/string, then (a digit (0-9), then one of the characters -, optionally (zero or one time)), between 12 and 18 times, then a digit (0-9), then the end of the line/string.
Limit: Checks the shape only, not the Luhn checksum or whether the card exists.
Examples
Matches
- ✓ 4111 1111 1111 1111
- ✓ 5500-0000-0000-0004
- ✓ 378282246310005
- ✓ 6011000990139424
Does not match
- ✗ 4111 1111 1111
- ✗ 4111--1111-1111-1111
- ✗ 4111 1111 1111 111a
- ✗ card: none
Try it
0 matches (JavaScript engine)
4111 1111 1111 1111
5500-0000-0000-0004
378282246310005
6011000990139424
4111 1111 1111
4111--1111-1111-1111
4111 1111 1111 111a
card: noneCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:\d[ -]?){12,18}\d$/g;
const text = "4111 1111 1111 1111";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:\d[ -]?){12,18}\d$')
text = '4111 1111 1111 1111'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:\d[ -]?){12,18}\d$/';
$text = '4111 1111 1111 1111';
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("^(?:\\d[ -]?){12,18}\\d$");
String text = "4111 1111 1111 1111";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:\d[ -]?){12,18}\d$`)
text := "4111 1111 1111 1111"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:\d[ -]?){12,18}\d$");
var text = "4111 1111 1111 1111";
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"^(?:\d[ -]?){12,18}\d$").unwrap();
let text = "4111 1111 1111 1111";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:\d[ -]?){12,18}\d$/
text = '4111 1111 1111 1111'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this credit card regex run the Luhn check?
- No. A regular expression cannot do the arithmetic the Luhn algorithm needs. Use this pattern as a first filter, then run a Luhn function in code before you treat the number as plausible.
- Why does it allow 13 to 19 digits instead of exactly 16?
- Card numbers vary by network. Older Visa numbers have 13 digits, Amex has 15, most cards have 16, and some debit and UnionPay cards reach 19. A generic shape check has to cover that whole range.
- Can I use this to redact card numbers in logs?
- Yes, but remove the ^ and $ anchors and add the g flag so it can find card-shaped runs inside longer lines. Expect some false positives such as long order IDs, so review what gets masked.
Related patterns
- American Express card number shape^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$
- Mastercard number shape^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$
- Visa card number shape^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$
- Accounting negative amount in parentheses^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$