MasterCard facilitates electronic funds transfers throughout the world, most commonly through MasterCard-branded credit cards, debit cards and prepaid cards. They don’t issue cards, but, they provide financial institutions ability to launch MasterCard branded cards. In this article let’s understand how we can create a regex for MasterCard number and how regex can be matched for MasterCard number.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of MasterCard
- It should be 16 digits long.
- It should not contain any alphabet or special characters.
- It should start with either two digits numbers may range from 51 to 55 or four digits numbers may range from 2221 to 2720.
- In the case of 51 to 55, the next 14 digits should be any number between 0-9.
- In the case of 2221 to 2720, the next 12 digits should be any number between 0-9.
Regex for checking if MasterCard is valid or not
Regular Expression-
/^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
6103436443236543 | does not match |
5503245678654335 | matches |
3221345643568765 | does not match |
2720675432565433 | matches |
Here is a detailed explanation of the above regex-
/^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/gm
^ asserts position at start of a line
Non-capturing group (?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)
1st Alternative 5[1-5][0-9]{2}
5 matches the character 5 with index 5310 (3516 or 658) literally (case sensitive)
Match a single character present in the list below [1-5]
1-5 matches a single character in the range between 1 (index 49) and 5 (index 53) (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative 222[1-9]
222 matches the characters 222 literally (case sensitive)
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
3rd Alternative 22[3-9][0-9]
22 matches the characters 22 literally (case sensitive)
Match a single character present in the list below [3-9]
3-9 matches a single character in the range between 3 (index 51) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
4th Alternative 2[3-6][0-9]{2}
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below [3-6]
3-6 matches a single character in the range between 3 (index 51) and 6 (index 54) (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
5th Alternative 27[01][0-9]
27 matches the characters 27 literally (case sensitive)
Match a single character present in the list below [01]
01 matches a single character in the list 01 (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
6th Alternative 2720
2720 matches the characters 2720 literally (case sensitive)
Match a single character present in the list below [0-9]
{12} matches the previous token exactly 12 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to match MasterCard card number regex pattern. MasterCard’s global financial network empowers electronic transactions via credit, debit, and prepaid cards. While not directly issuing cards, MasterCard enables financial institutions to launch their branded cards. In this article, we explored crafting a regex pattern for validating MasterCard numbers. Regex, a versatile tool, aids in text processing, validation, and manipulation across programming, editing, and command line tasks.