How to use REGEX to detect credit card(Part I)

This 2- part tutorial series covers building and deploying a Java EE app for detecting credit card info(s) using the following tools

 

  • Java7&8
  • Java EE 7
  • Maven
  • Apache Tomcat for local deployment
  • Github (Software Development Platform)
  • Heroku (Cloud Application Platform)

We will be building something like this.

 

The part I of this tutorial will be in console while the part II will be in Java EE

 

Part I

1 : Project structure “Create new Java EE project with Maven from CMD or any IDE”

Project struture

2 : Credit Card REGEX and Luhn algorithm

Cards.java

package com.dzone.cardinfo;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;
import java.util.stream.Stream;
public enum Cards {
    Unknown(Pattern.compile("^unknown$")),
    Visa(Pattern.compile("^4[0-9]{6,}$")),
    MasterCard(Pattern.compile("^5[1-5][0-9]{5,}$|^(222[1-9]|2[3-6][0-9][0-9]|27[0-1][0-9]|2720)[0-9]{12}$")),
    AmericanExpress(Pattern.compile("^3[47][0-9]{5,}$")),
    DinersClub(Pattern.compile("^3(?:0[0-5]|[68][0-9])[0-9]{4,}$")),
    Discover(Pattern.compile("^6(?:011|5[0-9]{2})[0-9]{3,}$")),
    JCB(Pattern.compile("^(?:2131|1800|35[0-9]{3})[0-9]{3,}$")),
    Amex(Pattern.compile("^3[47][0-9]{13}$")),
    CarteBlanche(Pattern.compile("^389[0-9]{11}$")),
    KoreanLocalCards(Pattern.compile("^9[0-9]{15}$")),
    BCGlobal(Pattern.compile("^(6541|6556)[0-9]{12}$"));
    protected static Cards type(final String cardNumber){
        return Stream.of(values())
                .filter( (cardNum) -> (cardNum.pattern.matcher(cardNumber).matches()) )
                .findAny()
                .orElse(Unknown);
    }
    private final Pattern pattern;
    private Cards(final Pattern pattern){
        this.pattern = pattern;
    }

    protected static String luhnCheck(String cardNumber) {
        int sum = 0;
        boolean alternate = false;
        for (int i = cardNumber.length() - 1; i >= 0; i--) {
            int n = Integer.parseInt(cardNumber.substring(i, i + 1));
            if (alternate) {
                n *= 2;
                if (n > 9) {
                    n = (n % 10) + 1;
                }
            }
            sum += n;
            alternate = !alternate;
        }
        return String.format("  "+ (sum % 10 == 0 ? "Valid" : "Invalid") );
    }
    protected static String getDateAndTime() throws Exception {
        return String.format("%s ", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd @ hh:mm:ss")));
    }
    protected static String info(String data) throws Exception{
        return "Card Type : " +type(data) + ", Luhn Check : " +luhnCheck(data) + " On "+getDateAndTime();
    }
}

3:Run on console

App.java

package com.dzone.cardinfo;
import com.dzone.cardinfo.Cards;
public class App {
    public static void main ( String[] args) throws Exception {
		System.out.println(Cards.info("5223223232323223"));
        System.out.println(Cards.info("237823782873738732"));
        System.out.println(Cards.info("789327636276367236"));
	}
}

output


Card Type : MasterCard, Luhn Check :   Invalid On 2017-08-02 @ 11:51:44 
Card Type : Unknown, Luhn Check :   Valid On 2017-08-02 @ 11:51:44 
Card Type : Unknown, Luhn Check :   Invalid On 2017-08-02 @ 11:51:44 

I hope you have seen how to detect credit card info(s) with Java

References

  1. http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Maven_EE/MavenEE.html
  2. Bank card number
  3. http://www.admfactory.com/luhn-algorithm-implementation-in-java/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: