Run Application
Create new java file named Main.java. This file use Regular Expression check validate Credit Card Number as below:
package regular_expression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private static boolean isValid(String creditCardNumber) {
String creditCardNumberPattern = "((?:(?:\\d{4}[- ]){3}\\d{4}|\\d{16}))(?![\\d])";
Pattern pattern = Pattern.compile(creditCardNumberPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(creditCardNumber);
return matcher.matches();
}
public static void main(String[] args) {
String creditCardNumber1 = "44444444444abc";
boolean result1 = isValid(creditCardNumber1);
System.out.println("Result 1: " + result1);
String creditCardNumber2 = "4444444444444448";
boolean result2 = isValid(creditCardNumber2);
System.out.println("Result 2: " + result2);
}
}
Output
Result 1: false
Result 2: true