Here you can find the source of FindCreditCard(String AccountNumber)
public static int FindCreditCard(String AccountNumber)
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static final int VISA = 0; public static final int MASTERCARD = 1; public static final int AMERICAN_EXPRESS = 2; public static final int DISCOVER = 3; public static final int DINERS_CLUB = 4; public static final int JCB = 5; public static final int NOT_RECOGNIZED = 6; public static int FindCreditCard(String AccountNumber) { if (AccountNumber == null) { return NOT_RECOGNIZED; }// w w w . j a va2s .c o m AccountNumber = UnFormatAccountNumber(AccountNumber); AccountNumber = AccountNumber.trim(); if (AccountNumber.length() == 0) { return NOT_RECOGNIZED; } if (IsVISACreditCard(AccountNumber)) { return VISA; } else if (IsMasterCardCreditCard(AccountNumber)) { return MASTERCARD; } else if (IsAmericanExpressCreditCard(AccountNumber)) { return AMERICAN_EXPRESS; } else if (IsDinersClubCreditCard(AccountNumber)) { return DINERS_CLUB; } else if (IsDiscoverCreditCard(AccountNumber)) { return DISCOVER; } else if (IsJCBCreditCard(AccountNumber)) { return JCB; } else { return NOT_RECOGNIZED; } } public static String UnFormatAccountNumber(String AccountNumber) { /* StringBuilder sb = new StringBuilder(); Pattern p = Pattern.compile("(\\d+)"); Matcher m = p.matcher(AccountNumber); while (m.find()) { sb.append(m.group(1)); } return sb.toString();*/ String result = AccountNumber; if (containsNonNumericCharacters(AccountNumber)) { StringBuilder sb = new StringBuilder(); Pattern p = Pattern.compile("(\\d+)"); Matcher m = p.matcher(AccountNumber); while (m.find()) { sb.append(m.group(1)); } result = sb.toString(); } return result; } private static boolean IsVISACreditCard(String UnformattedAccountNumber) { Pattern p = Pattern.compile("^4[0-9]{12}(?:[0-9]{3})?$"); Matcher m = p.matcher(UnformattedAccountNumber); return m.matches(); } private static boolean IsMasterCardCreditCard( String UnformattedAccountNumber) { Pattern p = Pattern.compile("^5[1-5][0-9]{14}$"); Matcher m = p.matcher(UnformattedAccountNumber); return m.matches(); } private static boolean IsAmericanExpressCreditCard( String UnformattedAccountNumber) { Pattern p = Pattern.compile("^3[47][0-9]{13}$"); Matcher m = p.matcher(UnformattedAccountNumber); return m.matches(); } private static boolean IsDinersClubCreditCard( String UnformattedAccountNumber) { Pattern p = Pattern.compile("^3(?:0[0-5]|[68][0-9])[0-9]{11}$"); Matcher m = p.matcher(UnformattedAccountNumber); return m.matches(); } private static boolean IsDiscoverCreditCard( String UnformattedAccountNumber) { Pattern p = Pattern.compile("^6(?:011|5[0-9]{2})[0-9]{12}$"); Matcher m = p.matcher(UnformattedAccountNumber); return m.matches(); } private static boolean IsJCBCreditCard(String UnformattedAccountNumber) { Pattern p = Pattern.compile("^(?:2131|1800|35\\d{3})\\d{11}$"); Matcher m = p.matcher(UnformattedAccountNumber); return m.matches(); } public static boolean containsNonNumericCharacters(String rawInput) { Pattern p = Pattern.compile("(\\D+)"); Matcher m = p.matcher(rawInput); return m.find(); } }