import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\b(5[1-5][0-9]{2}[ -])([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b(5[1-5][0-9]{2})([0-9]{4}){3}\\b|\\b222[1-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b222[1-9]([0-9]{4}){3}\\b|\\b22[3-9][0-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b22[3-9][0-9]([0-9]{4}){3}\\b|\\b2[3-6][0-9]{2}[ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b2[3-6][0-9]{2}([0-9]{4}){3}\\b|\\b27[01][0-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b27[01][0-9]([0-9]{4}){3}\\b|\\b2720[ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b2720([0-9]{4}){3}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){2}[0-9]\\b|\\b4[0-9][0-9]{11}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){2}(?:[0-9]{4})?$\\b|\\b4[0-9][0-9]{14}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){3}(?:[0-9]{3})?$\\b|\\b4[0-9][0-9]{17}\\b|\\b(34[0-9]{2}[ -])([0-9]{6}[ -])[0-9]{5}\\b|\\b34[0-9]{13}\\b|\\b(37[0-9]{2}[ -])([0-9]{6}[ -])[0-9]{5}\\b|\\b37[0-9]{13}\\b|\\b6(?:011|5[0-9]{2})[ -]([0-9]{4}[ -]){2}([0-9]{4})\\b|\\b6(?:011|5[0-9]{2})[0-9]{12}\\b";
final String string = "All Four major credit cards: MasterCard, Visa, American Express, Discover\n\n"
+ "Requirements:\n"
+ "1) MasterCard numbers either start with the numbers 51 through 55 or with the numbers 2221 through 2720. All have 16 digits.\n\n"
+ "2) Visa numbers start with the number 4 and have either 13, 16, or 19 digits (new cards).\n\n"
+ "3) American Express cards start with the number 34 or 37 and have 15 digits. Pattern is 4-6-5 when having spaces or dashes.\n\n"
+ "4) Discover cards start with the number 6011 or 65 and have 16 digits. \n\n"
+ "Patterns:\n\n"
+ "1) MasterCard Pattern:\n"
+ "\\b(5[1-5][0-9]{2}[ -])([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b(5[1-5][0-9]{2})([0-9]{4}){3}\\b|\\b222[1-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b222[1-9]([0-9]{4}){3}\\b|\\b22[3-9][0-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b22[3-9][0-9]([0-9]{4}){3}\\b|\\b2[3-6][0-9]{2}[ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b2[3-6][0-9]{2}([0-9]{4}){3}\\b|\\b27[01][0-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b27[01][0-9]([0-9]{4}){3}\\b|\\b2720[ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b2720([0-9]{4}){3}\\b\n\n"
+ "2) Visa Pattern:\n"
+ "\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){2}[0-9]\\b|\\b4[0-9][0-9]{11}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){2}(?:[0-9]{4})?$\\b|\\b4[0-9][0-9]{14}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){3}(?:[0-9]{3})?$\\b|\\b4[0-9][0-9]{17}\\b\n\n"
+ "3) American Express Pattern:\n"
+ "\\b(34[0-9]{2}[ -])([0-9]{6}[ -])[0-9]{5}\\b|\\b34[0-9]{13}\\b|\\b(37[0-9]{2}[ -])([0-9]{6}[ -])[0-9]{5}\\b|\\b37[0-9]{13}\\b\n\n"
+ "4) Discover Pattern:\n"
+ "\\b6(?:011|5[0-9]{2})[ -]([0-9]{4}[ -]){2}([0-9]{4})\\b|\\b6(?:011|5[0-9]{2})[0-9]{12}\\b\n\n"
+ "Combining all Four Major Company Patterns:\n"
+ "\\b(5[1-5][0-9]{2}[ -])([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b(5[1-5][0-9]{2})([0-9]{4}){3}\\b|\\b222[1-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b222[1-9]([0-9]{4}){3}\\b|\\b22[3-9][0-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b22[3-9][0-9]([0-9]{4}){3}\\b|\\b2[3-6][0-9]{2}[ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b2[3-6][0-9]{2}([0-9]{4}){3}\\b|\\b27[01][0-9][ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b27[01][0-9]([0-9]{4}){3}\\b|\\b2720[ -]([0-9]{4}[ -]){2}[0-9]{4}\\b|\\b2720([0-9]{4}){3}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){2}[0-9]\\b|\\b4[0-9][0-9]{11}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){2}(?:[0-9]{4})?$\\b|\\b4[0-9][0-9]{14}\\b|\\b(4[0-9]{3}[ -])([0-9]{4}[ -]){3}(?:[0-9]{3})?$\\b|\\b4[0-9][0-9]{17}\\b|\\b(34[0-9]{2}[ -])([0-9]{6}[ -])[0-9]{5}\\b|\\b34[0-9]{13}\\b|\\b(37[0-9]{2}[ -])([0-9]{6}[ -])[0-9]{5}\\b|\\b37[0-9]{13}\\b|\\b6(?:011|5[0-9]{2})[ -]([0-9]{4}[ -]){2}([0-9]{4})\\b|\\b6(?:011|5[0-9]{2})[0-9]{12}\\b\n\n\n\n\n\n"
+ "#####################################################################################################\n"
+ "MasterCard Test Cases:\n"
+ "Positive (should fire):\n"
+ "16 length, no space, start with 51-55 Here is the cc: 5123123412341234 Thanks.\n"
+ "16 length, dashes, start with 51-55 5123-1234-1234-1234\n"
+ "16 length, spaces, start with 51-55 5123 1234 1234 1234\n\n"
+ "16 length, no space, start with 2221-2720 2221123412341234\n"
+ "16 length, dashes, start with 2221-2720 2221-1234-1234-1234\n"
+ "16 length, spaces, start with 2221-2720 2221 1234 1234 1234\n\n"
+ "16 length, no space, start with 2221-2720 2241123412341234\n"
+ "16 length, dashes, start with 2221-2720 2241-1234-1234-1234\n"
+ "16 length, spaces, start with 2221-2720 2241 1234 1234 1234\n\n"
+ "16 length, no space, start with 2221-2720 2321123412341234\n"
+ "16 length, dashes, start with 2221-2720 2321-1234-1234-1234\n"
+ "16 length, spaces, start with 2221-2720 2321 1234 1234 1234\n\n"
+ "16 length, no space, start with 2221-2720 2705123412341234\n"
+ "16 length, dashes, start with 2221-2720 2705-1234-1234-1234\n"
+ "16 length, spaces, start with 2221-2720 2705 1234 1234 1234\n\n"
+ "16 length, no space, start with 2221-2720 2720123412341234\n"
+ "16 length, dashes, start with 2221-2720 2720-1234-1234-1234\n"
+ "16 length, spaces, start with 2221-2720 2720 1234 1234 1234\n\n"
+ "16 length, no space, start with 51-55 Here is the cc: 5123123412341234\n"
+ "16 length, dashes, start with 51-55 Here is the cc: 5123-1234-1234-1234\n"
+ "16 length, spaces, start with 51-55 Here is the cc: 5123 1234 1234 1234\n\n"
+ "16 length, no space, start with 51-55 Here is the card#:5123123412341234\n"
+ "16 length, dashes, start with 51-55 Here is the card#:5123-1234-1234-1234\n"
+ "16 length, spaces, start with 51-55 Here is the card#:5123 1234 1234 1234\n\n"
+ "Negative (should not fire):\n"
+ "16 length, no space, not start with 51-55 5623123412341234\n"
+ "16 length, dashes, not start with 51-55 5623-1234-1234-1234\n"
+ "16 length, spaces, not start with 51-55 5623 1234 1234 1234\n\n"
+ "16 length, no space, not start with 2221-2720 2220123412341234\n"
+ "16 length, dashes, not start with 2221-2720 2220-1234-1234-1234\n"
+ "16 length, spaces, not start with 2221-2720 2220 1234 1234 1234\n\n"
+ "Non 16 length, matches 51 pattern start 5123-1234-1234-123\n"
+ "Non 16 length, matches 2221 pattern start 2221-1234-1234-123\n\n"
+ "16 length, no space, start with 51-55, embedded Credit Card Number5123123412341234\n"
+ "16 length, dashes, start with 51-55, malformed 512-312-34123-41234\n"
+ "16 length num matching pattern but embedded User ID: 0000005123123412341234000000 \n"
+ "#####################################################################################################\n"
+ "Visa Test Cases:\n"
+ "Positive (should fire):\n"
+ "13 length, no space, start with 4 Here is the cc: 4123123412341 Thanks.\n"
+ "13 length, dashes, start with 4 4123-1234-1234-1\n"
+ "13 length, spaces, start with 4 4123 1234 1234 1\n\n"
+ "16 length, no space, start with 4 4221123412341234\n"
+ "16 length, dashes, start with 4 4221-1234-1234-1234\n"
+ "16 length, spaces, start with 4 4221 1234 1234 1234\n\n"
+ "19 length, no space, start with 4 4241123412341234123\n"
+ "19 length, dashes, start with 4 4241-1234-1234-1234-123\n"
+ "19 length, spaces, start with 4 4241 1234 1234 1234-123\n\n"
+ "Embedded in sentence with space:\n"
+ "13 length, no space, start with 4 Here is the cc: 4123123412341\n"
+ "13 length, dashes, start with 4 Here is the cc: 4123-1234-1234-1\n"
+ "13 length, spaces, start with 4 Here is the cc: 4123 1234 1234 1\n\n"
+ "16 length, no space, start with 4 Here is the cc: 4221123412341234\n"
+ "16 length, dashes, start with 4 Here is the cc: 4221-1234-1234-1234\n"
+ "16 length, spaces, start with 4 Here is the cc: 4221 1234 1234 1234\n\n"
+ "19 length, no space, start with 4 Here is the cc: 4241123412341234123\n"
+ "19 length, dashes, start with 4 Here is the cc: 4241-1234-1234-1234-123\n"
+ "19 length, spaces, start with 4 Here is the cc: 4241 1234 1234 1234-123\n\n"
+ "13 length, no space, start with 4 Here is the card#:4123123412341\n"
+ "13 length, dashes, start with 4 Here is the card#:4123-1234-1234-1\n"
+ "13 length, spaces, start with 4 Here is the card#:4123 1234 1234 1\n\n"
+ "16 length, no space, start with 4 Here is the card#:4221123412341234\n"
+ "16 length, dashes, start with 4 Here is the card#:4221-1234-1234-1234\n"
+ "16 length, spaces, start with 4 Here is the card#:4221 1234 1234 1234\n\n"
+ "19 length, no space, start with 4 Here is the card#:4241123412341234123\n"
+ "19 length, dashes, start with 4 Here is the card#:4241-1234-1234-1234-123\n"
+ "19 length, spaces, start with 4 Here is the card#:4241 1234 1234 1234-123\n\n"
+ "Negative (should not fire):\n"
+ "13 length, no space, not start with 4 0123123412341\n"
+ "13 length, dashes, not start with 4 0123-1234-1234-1\n"
+ "13 length, spaces, not start with 4 0123 1234 1234 1\n\n"
+ "16 length, no space, not start with 4 0221123412341234\n"
+ "16 length, dashes, not start with 4 0221-1234-1234-1234\n"
+ "16 length, spaces, not start with 4 0221 1234 1234 1234\n\n"
+ "19 length, no space, not start with 4 0241123412341234123\n"
+ "19 length, dashes, not start with 4 0241-1234-1234-1234-123\n"
+ "19 length, spaces, not start with 4 0241 1234 1234 1234-123\n\n"
+ "13 length, no space, start with 4, embedded Credit Card Number4123123412341\n"
+ "13 length, dashes, start with 4, malformed 412-31234-12341\n"
+ "13 length num matching pattern but embedded User ID: 0000004123123412341000000 \n"
+ "16 length, no space, start with 4, embedded Credit Card Number4123123412341234\n"
+ "16 length, dashes, start with 4, malformed 412-312-34123-41234\n"
+ "16 length num matching pattern but embedded User ID: 0000004123123412341234000000\n\n"
+ "19 length, no space, start with 4, embedded Credit Card Number4123123412341234123\n"
+ "19 length, dashes, start with 4, malformed 412-312341-234123-4123\n"
+ "19 length num matching pattern but embedded User ID: 0000004123123412341234123000000\n\n"
+ "#####################################################################################################\n"
+ "American Express Test Cases:\n"
+ "Positive (Should fire):\n"
+ "15 length, no space, start with 34 Here is the cc: 342312341234123 Thanks.\n"
+ "15 length, dashes, start with 34 3423-123456-12345 \n"
+ "15 length, spaces, start with 34 3423 123456 12345 \n\n"
+ "15 length, no space, start with 37 372112341234123\n"
+ "15 length, dashes, start with 37 3721-123456-12345\n"
+ "15 length, spaces, start with 37 3721 123456 12345\n\n"
+ "Embedded in sentence with space:\n"
+ "15 length, no space, start with 34 Here is the cc: 342312341234112\n"
+ "15 length, dashes, start with 34 Here is the cc: 3423-123456-12345\n"
+ "15 length, spaces, start with 34 Here is the cc: 3423 123456 12345\n\n"
+ "15 length, no space, start with 37 Here is the cc: 372112341234123\n"
+ "15 length, dashes, start with 37 Here is the cc: 3721-123456-12345\n"
+ "15 length, spaces, start with 37 Here is the cc: 3721 123456 12345\n\n"
+ "15 length, no space, start with 34 Credit Card#:342312341234112\n"
+ "15 length, dashes, start with 34 Credit Card#:3423-123456-12345\n"
+ "15 length, spaces, start with 34 Credit Card#:3423 123456 12345\n\n"
+ "15 length, no space, start with 37 Credit Card#:372112341234123\n"
+ "15 length, dashes, start with 37 Credit Card#:3721-123456-12345\n"
+ "15 length, spaces, start with 37 Credit Card#:3721 123456 12345\n\n"
+ "Negative (Should Not Fire):\n"
+ "15 length, no space, not start with 34 352312341234112\n"
+ "15 length, dashes, not start with 34 3523-123456-12345\n"
+ "15 length, spaces, not start with 34 3523 123456 12345\n\n"
+ "15 length, no space, not start with 37 362112341234123\n"
+ "15 length, dashes, not start with 37 3621-123456-12345\n"
+ "15 length, spaces, not start with 37 3621 123456 12345\n\n"
+ "15 length, no space, start with 34, embedded Credit Card Number342312341234112\n"
+ "15 length, dashes, start with 34, malformed 3423-1234-1234-112\n\n"
+ "15 length num matching pattern but embedded User ID: 000000342312341234112000000\n\n"
+ "#####################################################################################################\n"
+ "Discover Test Cases:\n"
+ "Positive (Should fire):\n"
+ "16 length, no space, start with 6011 Here is the cc: 6011123412341234 Thanks.\n"
+ "16 length, dashes, start with 6011 6011-1234-1234-1234\n"
+ "16 length, spaces, start with 6011 6011 1234 1234 1234\n\n"
+ "16 length, no space, start with 65 6521123412341234\n"
+ "16 length, dashes, start with 65 6521-1234-1234-1234\n"
+ "16 length, spaces, start with 65 6521 1234 1234 1234\n\n"
+ "Embedded in sentence with space:\n"
+ "16 length, no space, start with 6011 Here is the cc: 6011123412341234\n"
+ "16 length, dashes, start with 6011 Here is the cc: 6011-1234-1234-1234\n"
+ "16 length, spaces, start with 6011 Here is the cc: 6011 1234 1234 1234\n\n"
+ "16 length, no space, start with 65 Here is the cc: 6521123412341234\n"
+ "16 length, dashes, start with 65 Here is the cc: 6521-1234-1234-1234\n"
+ "16 length, spaces, start with 65 Here is the cc: 6523 1234 1234 1234\n\n"
+ "16 length, no space, start with 6011 Credit Card#:6011123412341234\n"
+ "16 length, dashes, start with 6011 Credit Card#:6011-1234-1234-1234\n"
+ "16 length, spaces, start with 6011 Credit Card#:6011 1234 1234 1234\n\n"
+ "16 length, no space, start with 65 Credit Card#:6521123412341234\n"
+ "16 length, dashes, start with 65 Credit Card#:6521-1234-1234-1234\n"
+ "16 length, spaces, start with 65 Credit Card#:6523 1234 1234 1234\n\n"
+ "Negative (Should Not Fire):\n"
+ "16 length, no space, not start with 6011 6012123412341234\n"
+ "16 length, dashes, not start with 6011 6012-1234-1234-1234\n"
+ "16 length, spaces, not start with 6011 6012 1234 1234 1234\n\n"
+ "16 length, no space, not start with 65 6612123412341234\n"
+ "16 length, dashes, not start with 65 6612-1234-1234-1234\n"
+ "16 length, spaces, not start with 65 6612-1234-1234-1234\n\n"
+ "16 length, no space, start with 65, embedded Credit Card Number6521123412341234\n"
+ "16 length, dashes, start with 6011, malformed 601-11234-1234-1234\n\n"
+ "16 length num matching pattern but embedded User ID: 0000006011123412341234000000\n";
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));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html