How to validate MasterCard number using Regular Expression

Given a string str, the task is to check whether the given string is valid Master Card number or not by using Regular Expression.
The valid Master Card number must satisfy the following conditions.

  1. It should be 16 digits long.
  2. It should start with either two digits number may range from 51 to 55 or four digits number may range from 2221 to 2720.
  3. In the case of 51 to 55, the next fourteen digits should be any number between 0-9.
  4. In the case of 2221 to 2720, the next twelve digits should be any number between 0-9.
  5. It should not contains any alphabets and special characters.

Examples:

Input: str = “5114496353984312”;
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore it is a valid Master Card number.

Input: str = “5582822410”;
Output: false
Explanation:
The given string has 10 digits. Therefore it is not a valid Master Card number.

Input: str = “6082822463100051”;
Output: false
Explanation:
The given string does not starts with either two digits number may range from 51 to 55 or four digits number may range from 2221 to 2720. Therefore it is not a valid valid Master Card number.



Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.

  1. Get the String.
  2. Create a regular expression to check valid Master Card number as mentioned below:

    regex = “^5[1-5][0-9]{14}|^(222[1-9]|22[3-9]\\d|2[3-6]\\d{2}|27[0-1]\\d|2720)[0-9]{12}$”;

    Where:

    • ^ represents the starting of the string.
    • 5[1-5] represents the first two digit number may range from 51 to 55.
    • [0-9]{14} represents the next fourteen digits should be any between 0-9.
    • | represents the or.
    • ( represents the starting of the group.
    • 222[1-9] represents the first four digit number may range from 2221 to 2229.
    • | represents the or.
    • 22[3-9]\\d represents the first four digit number may range from 2230 to 2299.
    • | represents the or.
    • 2[3-6]\\d{2} represents the first four digit number may range from 2300 to 2699.
    • | represents the or.
    • 27[0-1]\\d represents the first four digit number may range from 2700 to 2719.
    • | represents the or.
    • 2720 represents the first four digit may starts with 2720.
    • ) represents the ending of the group.
    • [0-9]{12} represents the next twelve digits should be any between 0-9.
    • $ represents the ending of the string.
  3. Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher()
  4. Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to validate
// Master Card number
// using regular expression
  
import java.util.regex.*;
class GFG {
  
    // Function to validate
    // Master Card number
    // using regular expression.
    public static boolean
    isValidMasterCardNo(String str)
    {
  
        // Regex to check valid
        // Master Card number.
        String regex = "^5[1-5][0-9]{14}|"
                       + "^(222[1-9]|22[3-9]\\d|"
                       + "2[3-6]\\d{2}|27[0-1]\\d|"
                       + "2720)[0-9]{12}$";
  
        // Compile the ReGex
        Pattern p
            = Pattern.compile(regex);
  
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
  
        // Find match between given string
        // and regular expression
        // using Pattern.matcher()
        Matcher m = p.matcher(str);
  
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Test Case 1:
        String str1 = "5114496353984312";
        System.out.println(
            isValidMasterCardNo(str1));
  
        // Test Case 2:
        String str2 = "2720822463109651";
        System.out.println(
            isValidMasterCardNo(str2));
  
        // Test Case 3:
        String str3 = "5582822410";
        System.out.println(
            isValidMasterCardNo(str3));
  
        // Test Case 4:
        String str4 = "6082822463100051";
        System.out.println(
            isValidMasterCardNo(str4));
  
        // Test Case 5:
        String str5 = "2221149a635##843";
        System.out.println(
            isValidMasterCardNo(str5));
    }
}

chevron_right


Output:

true
true
false
false
false

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.