Regular Expression Java - Find the Card Numbers


1. Find the Card Numbers - Regular Expression Example


A conversation passage between a manager and a customer in a bank will be given.

There will be many card numbers given in the passage. You should find the card numbers of a given card type.
There are four types of cards. The card names and their number formats are given as follows.

 

Card Name Number Format
Visa  Starts with 4 and can have a length of either 13 or 16
American ExpressStarts with 34 or 37 and has a length of 15
Discover  Starts with 6011 or 65 and has a length of 16
JCB Starts with 2131 or 1800 and has a length of 15 or starts with 35 and has a length of 16

 

Use regex to find out the card numbers in the passage that matches the given type of card.

 

Input Specifications:
    The first line consists of a string.
    The next line consists of the type of card to be searched.

 

Output Specifications:
    A string having the numbers that match the card type with the numbers separated by a space.
    If the card type is not in the above mentioned list of cards, then print "Not a valid card type".
    
Sample Input 1:
Manager: Good morning, sir. How can I help you?

Customer: Sir, the card names in my cards have got scratched, and I am unable to find their types. Would you be able to help me find the type using the card numbers, please?

Manager: What are the types of cards you have, sir?

Customer: I have Visa, American Express, Discover, and JCB cards.

Manager: Okay, sir. Can I have your account number and mobile number?

Customer: My account number is 1356798988878, and my mobile number is 8765465743.

Manager: Can you tell me the card numbers, sir?

Customer: The card numbers are 4769856453217, 340987687654689, 213189796876789, 6011675489757876, and 3589797987979879.

Manager: Please wait a minute, sir.

Customer: There are two other cards as well. The numbers are 4654465765767878 and 6589456879087568.

Manager: Please give me a minute, sir.
Visa

 

Sample Output 1:
4769856453217 4654465765767878

 

Explanation: 
    In the given passage, we have two card numbers starting with 4 and having a length of 13 and 16. Hence, we print those card numbers.

 

Sample Input 2: 

Manager: Good morning, sir. How can I help you?

Customer: Sir, the card names in my cards have got scratched, and I am unable to find their types. Would you be able to help me find the type using the card numbers, please?

Manager: What are the types of cards you have, sir?

Customer: I have Visa, American Express, Discover, and JCB cards.

Manager: Okay, sir. Can I have your account number and mobile number?

Customer: My account number is 1356798988878, and my mobile number is 8765465743.

Manager: Can you tell me the card numbers, sir?

Customer: The card numbers are 4769856453217, 340987687654689, 213189796876789, 6011675489757876, and 3589797987979879.

Manager: Please wait a minute, sir.

Customer: There are two other cards as well. The numbers are 4654465765767878 and 6589456879087568.

Manager: Please give me a minute, sir.

Credit

 

Sample Output 2:
Not a valid card type

 

Explanation: 
The given card type is Credit, which is not one of the given card types. Therefore, "Not a valid card type" should be printed as the output.


    


    RegEX.java


package com.fresco;

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegEx {

    public String findCardTypeNumbers(String conversationString cardType) {
        Pattern pattern;
        Matcher matcher;
        StringBuilder res = new StringBuilder();
        if (cardType.equals("JCB")) {
            pattern = Pattern.compile("(2131|1800)\\d{11}|(35\\d{14})");
            matcher = pattern.matcher(conversation);
            while (matcher.find()) {
                res.append(matcher.group() + " ");
            }
            return res.toString().trim();
        } else if (cardType.equals("Visa")) {
            pattern = Pattern.compile("\\b(4\\d{12,15})\\b");
            matcher = pattern.matcher(conversation);
            while (matcher.find()) {
                res.append(matcher.group() + " ");
            }
            return res.toString().trim();
        } else if (cardType.equals("American Express")) {
            pattern = Pattern.compile("((34)|(37))\\d{13}");
            matcher = pattern.matcher(conversation);
            if (matcher.find()) {
                return matcher.group();
            } else {
                return "";
            }
        } else if (cardType.equals("Discover")) {
            pattern = Pattern.compile("(6011\\d{12})|(65\\d{14})");
            matcher = pattern.matcher(conversation);
            while (matcher.find()) {
                res.append(matcher.group() + " ");
            }
            return res.toString().trim();
        } else {
            return "Not a valid card type";
        }
    }


Comments