I am Passing creditCardNumber as 4242***4242 which is masked. How I can get Card Type based on Masked credit card number?
String regVisa = "^4[0-9]{2}(?:[0-9]{3})?$";
String reVisa = "(?:4[0-9]{12}(?:[0-9]{3})?$)";
String regMaster = "^5[1-5][0-9]{14}$";
String regExpress = "^3[47][0-9]{13}$";
String regDiners = "^3(?:0[0-5]|[68][0-9])[0-9]{11}$";
String regDiscover = "^6(?:011|5[0-9]{2})[0-9]{12}$";
String regJCB= "^(?:2131|1800|35\\d{3})\\d{11}$";
if(creditCardNumber.matches(regVisa))
return "visa";
if (creditCardNumber.matches(regMaster))
return "mastercard";
if (creditCardNumber.matches(regExpress))
return "amex";
if (creditCardNumber.matches(regDiners))
return "DINERS";
if (creditCardNumber.matches(regDiscover))
return "discover";
if (creditCardNumber.matches(regJCB))
return "jcb";
if (creditCardNumber.matches(reVisa))
return "VISA";
return "invalid";
Maybe, you have to design some "masked" expression for each, such as:
^4[0-9]{2}[0-9]\\*{3}[0-9]{4}$
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class re{
public static void main(String[] args){
final String regex = "^4[0-9]{2}[0-9]\\*{3}[0-9]{4}$";
final String string = "4242***4242\n"
+ "5242***4242";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Full match: 4242***4242
If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com . If you'd like, you can also watch in this link , how it would match against some sample inputs.
jex.im visualizes regular expressions:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.