Regex for Mastercard Credit Card
Regex Pattern
^(?:5[1-5][0-9]{14}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{12})$Mastercard — 16 digits starting with 51-55 or 2221-2720
Quick Answer
The regex pattern for mastercard credit card is `^(?:5[1-5][0-9]{14}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{12})$`. Mastercard — 16 digits starting with 51-55 or 2221-2720. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 5555555555554444 | ✓ Matches |
| 5105105105105100 | ✓ Matches |
| 2221000000000009 | ✓ Matches |
| 4111111111111111 | ✗ No match |
| 5055555555554444 | ✗ No match |
| 1234567890123456 | ✗ No match |
Code Examples
javascript
const regex = /^(?:5[1-5][0-9]{14}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{12})$/;
const isValid = regex.test(value);python
import re
pattern = r'^(?:5[1-5][0-9]{14}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{12})$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^(?:5[1-5][0-9]{14}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{12})$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^(?:5[1-5][0-9]{14}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{12})$/', $value)) {
echo "valid";
}java
String pattern = "^(?:5[1-5][0-9]{14}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{12})$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
Visa Credit Card
Visa card — starts with 4, 13 or 16 digits total
American Express Card
Amex — 15 digits starting with 34 or 37
Any Credit Card
Visa, Mastercard, Amex, Discover, Diners
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits
URL (HTTP/HTTPS)
Validates HTTP and HTTPS URLs