Finance
American Express card number shape Regex
American Express cards have 15 digits, start with 34 or 37, and are printed in an unusual 4-6-5 grouping. This pattern follows that layout exactly, allowing a single space or dash at each group boundary or no separators at all. It helps a payment form recognise Amex early so it can ask for the four digit security code instead of three, and it keeps test data consistent. The pattern does not accept 4-4-4-3 groupings that some users type by habit, so normalise input first if that matters. It checks the shape only and does not run the Luhn checksum or contact any issuer.
Pattern (JavaScript)
/^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$/Starts at the start of the line/string, then the character "3", then one of the characters 47, then exactly 2 digits, then one of the characters -, optionally (zero or one time), then exactly 6 digits, then one of the characters -, optionally (zero or one time), then exactly 5 digits, then the end of the line/string.
Limit: Checks the shape only, not the Luhn checksum or whether the card exists.
Examples
Matches
- ✓ 378282246310005
- ✓ 3714 496353 98431
- ✓ 3400-000000-00009
Does not match
- ✗ 4111111111111111
- ✗ 37828224631000
- ✗ 3611 111111 11111
- ✗ 3782 8224 6310 005
Try it
0 matches (JavaScript engine)
378282246310005
3714 496353 98431
3400-000000-00009
4111111111111111
37828224631000
3611 111111 11111
3782 8224 6310 005Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$/g;
const text = "378282246310005";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$')
text = '378282246310005'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$/';
$text = '378282246310005';
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("^3[47]\\d{2}[ -]?\\d{6}[ -]?\\d{5}$");
String text = "378282246310005";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$`)
text := "378282246310005"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$");
var text = "378282246310005";
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"^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$").unwrap();
let text = "378282246310005";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$/
text = '378282246310005'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is Amex grouped 4-6-5 rather than in fours?
- American Express chose that layout for its embossed cards, and it has stayed on the printed card ever since. This pattern accepts separators only at those boundaries, which mirrors what is printed on the card.
- What security code length goes with an Amex card?
- Amex uses a four digit code printed on the front, while most other networks use three digits on the back. Detecting the Amex shape early lets a form label and size that field correctly.
- Are 378282246310005 and 371449635398431 safe to use in tests?
- Yes. They are publicly documented sandbox numbers that payment providers accept only in test mode. They are useful for fixtures because they also pass the Luhn checksum.
Related patterns
- Credit card number (generic shape)^(?:\d[ -]?){12,18}\d$
- 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})?\)$