package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)\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`)
var str = `All Four major credit cards: MasterCard, Visa, American Express, Discover
Requirements:
1) MasterCard numbers either start with the numbers 51 through 55 or with the numbers 2221 through 2720. All have 16 digits.
2) Visa numbers start with the number 4 and have either 13, 16, or 19 digits (new cards).
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.
4) Discover cards start with the number 6011 or 65 and have 16 digits.
Patterns:
1) MasterCard Pattern:
\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
2) Visa Pattern:
\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
3) American Express Pattern:
\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
4) Discover Pattern:
\b6(?:011|5[0-9]{2})[ -]([0-9]{4}[ -]){2}([0-9]{4})\b|\b6(?:011|5[0-9]{2})[0-9]{12}\b
Combining all Four Major Company Patterns:
\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
#####################################################################################################
MasterCard Test Cases:
Positive (should fire):
16 length, no space, start with 51-55 Here is the cc: 5123123412341234 Thanks.
16 length, dashes, start with 51-55 5123-1234-1234-1234
16 length, spaces, start with 51-55 5123 1234 1234 1234
16 length, no space, start with 2221-2720 2221123412341234
16 length, dashes, start with 2221-2720 2221-1234-1234-1234
16 length, spaces, start with 2221-2720 2221 1234 1234 1234
16 length, no space, start with 2221-2720 2241123412341234
16 length, dashes, start with 2221-2720 2241-1234-1234-1234
16 length, spaces, start with 2221-2720 2241 1234 1234 1234
16 length, no space, start with 2221-2720 2321123412341234
16 length, dashes, start with 2221-2720 2321-1234-1234-1234
16 length, spaces, start with 2221-2720 2321 1234 1234 1234
16 length, no space, start with 2221-2720 2705123412341234
16 length, dashes, start with 2221-2720 2705-1234-1234-1234
16 length, spaces, start with 2221-2720 2705 1234 1234 1234
16 length, no space, start with 2221-2720 2720123412341234
16 length, dashes, start with 2221-2720 2720-1234-1234-1234
16 length, spaces, start with 2221-2720 2720 1234 1234 1234
16 length, no space, start with 51-55 Here is the cc: 5123123412341234
16 length, dashes, start with 51-55 Here is the cc: 5123-1234-1234-1234
16 length, spaces, start with 51-55 Here is the cc: 5123 1234 1234 1234
16 length, no space, start with 51-55 Here is the card#:5123123412341234
16 length, dashes, start with 51-55 Here is the card#:5123-1234-1234-1234
16 length, spaces, start with 51-55 Here is the card#:5123 1234 1234 1234
Negative (should not fire):
16 length, no space, not start with 51-55 5623123412341234
16 length, dashes, not start with 51-55 5623-1234-1234-1234
16 length, spaces, not start with 51-55 5623 1234 1234 1234
16 length, no space, not start with 2221-2720 2220123412341234
16 length, dashes, not start with 2221-2720 2220-1234-1234-1234
16 length, spaces, not start with 2221-2720 2220 1234 1234 1234
Non 16 length, matches 51 pattern start 5123-1234-1234-123
Non 16 length, matches 2221 pattern start 2221-1234-1234-123
16 length, no space, start with 51-55, embedded Credit Card Number5123123412341234
16 length, dashes, start with 51-55, malformed 512-312-34123-41234
16 length num matching pattern but embedded User ID: 0000005123123412341234000000
#####################################################################################################
Visa Test Cases:
Positive (should fire):
13 length, no space, start with 4 Here is the cc: 4123123412341 Thanks.
13 length, dashes, start with 4 4123-1234-1234-1
13 length, spaces, start with 4 4123 1234 1234 1
16 length, no space, start with 4 4221123412341234
16 length, dashes, start with 4 4221-1234-1234-1234
16 length, spaces, start with 4 4221 1234 1234 1234
19 length, no space, start with 4 4241123412341234123
19 length, dashes, start with 4 4241-1234-1234-1234-123
19 length, spaces, start with 4 4241 1234 1234 1234-123
Embedded in sentence with space:
13 length, no space, start with 4 Here is the cc: 4123123412341
13 length, dashes, start with 4 Here is the cc: 4123-1234-1234-1
13 length, spaces, start with 4 Here is the cc: 4123 1234 1234 1
16 length, no space, start with 4 Here is the cc: 4221123412341234
16 length, dashes, start with 4 Here is the cc: 4221-1234-1234-1234
16 length, spaces, start with 4 Here is the cc: 4221 1234 1234 1234
19 length, no space, start with 4 Here is the cc: 4241123412341234123
19 length, dashes, start with 4 Here is the cc: 4241-1234-1234-1234-123
19 length, spaces, start with 4 Here is the cc: 4241 1234 1234 1234-123
13 length, no space, start with 4 Here is the card#:4123123412341
13 length, dashes, start with 4 Here is the card#:4123-1234-1234-1
13 length, spaces, start with 4 Here is the card#:4123 1234 1234 1
16 length, no space, start with 4 Here is the card#:4221123412341234
16 length, dashes, start with 4 Here is the card#:4221-1234-1234-1234
16 length, spaces, start with 4 Here is the card#:4221 1234 1234 1234
19 length, no space, start with 4 Here is the card#:4241123412341234123
19 length, dashes, start with 4 Here is the card#:4241-1234-1234-1234-123
19 length, spaces, start with 4 Here is the card#:4241 1234 1234 1234-123
Negative (should not fire):
13 length, no space, not start with 4 0123123412341
13 length, dashes, not start with 4 0123-1234-1234-1
13 length, spaces, not start with 4 0123 1234 1234 1
16 length, no space, not start with 4 0221123412341234
16 length, dashes, not start with 4 0221-1234-1234-1234
16 length, spaces, not start with 4 0221 1234 1234 1234
19 length, no space, not start with 4 0241123412341234123
19 length, dashes, not start with 4 0241-1234-1234-1234-123
19 length, spaces, not start with 4 0241 1234 1234 1234-123
13 length, no space, start with 4, embedded Credit Card Number4123123412341
13 length, dashes, start with 4, malformed 412-31234-12341
13 length num matching pattern but embedded User ID: 0000004123123412341000000
16 length, no space, start with 4, embedded Credit Card Number4123123412341234
16 length, dashes, start with 4, malformed 412-312-34123-41234
16 length num matching pattern but embedded User ID: 0000004123123412341234000000
19 length, no space, start with 4, embedded Credit Card Number4123123412341234123
19 length, dashes, start with 4, malformed 412-312341-234123-4123
19 length num matching pattern but embedded User ID: 0000004123123412341234123000000
#####################################################################################################
American Express Test Cases:
Positive (Should fire):
15 length, no space, start with 34 Here is the cc: 342312341234123 Thanks.
15 length, dashes, start with 34 3423-123456-12345
15 length, spaces, start with 34 3423 123456 12345
15 length, no space, start with 37 372112341234123
15 length, dashes, start with 37 3721-123456-12345
15 length, spaces, start with 37 3721 123456 12345
Embedded in sentence with space:
15 length, no space, start with 34 Here is the cc: 342312341234112
15 length, dashes, start with 34 Here is the cc: 3423-123456-12345
15 length, spaces, start with 34 Here is the cc: 3423 123456 12345
15 length, no space, start with 37 Here is the cc: 372112341234123
15 length, dashes, start with 37 Here is the cc: 3721-123456-12345
15 length, spaces, start with 37 Here is the cc: 3721 123456 12345
15 length, no space, start with 34 Credit Card#:342312341234112
15 length, dashes, start with 34 Credit Card#:3423-123456-12345
15 length, spaces, start with 34 Credit Card#:3423 123456 12345
15 length, no space, start with 37 Credit Card#:372112341234123
15 length, dashes, start with 37 Credit Card#:3721-123456-12345
15 length, spaces, start with 37 Credit Card#:3721 123456 12345
Negative (Should Not Fire):
15 length, no space, not start with 34 352312341234112
15 length, dashes, not start with 34 3523-123456-12345
15 length, spaces, not start with 34 3523 123456 12345
15 length, no space, not start with 37 362112341234123
15 length, dashes, not start with 37 3621-123456-12345
15 length, spaces, not start with 37 3621 123456 12345
15 length, no space, start with 34, embedded Credit Card Number342312341234112
15 length, dashes, start with 34, malformed 3423-1234-1234-112
15 length num matching pattern but embedded User ID: 000000342312341234112000000
#####################################################################################################
Discover Test Cases:
Positive (Should fire):
16 length, no space, start with 6011 Here is the cc: 6011123412341234 Thanks.
16 length, dashes, start with 6011 6011-1234-1234-1234
16 length, spaces, start with 6011 6011 1234 1234 1234
16 length, no space, start with 65 6521123412341234
16 length, dashes, start with 65 6521-1234-1234-1234
16 length, spaces, start with 65 6521 1234 1234 1234
Embedded in sentence with space:
16 length, no space, start with 6011 Here is the cc: 6011123412341234
16 length, dashes, start with 6011 Here is the cc: 6011-1234-1234-1234
16 length, spaces, start with 6011 Here is the cc: 6011 1234 1234 1234
16 length, no space, start with 65 Here is the cc: 6521123412341234
16 length, dashes, start with 65 Here is the cc: 6521-1234-1234-1234
16 length, spaces, start with 65 Here is the cc: 6523 1234 1234 1234
16 length, no space, start with 6011 Credit Card#:6011123412341234
16 length, dashes, start with 6011 Credit Card#:6011-1234-1234-1234
16 length, spaces, start with 6011 Credit Card#:6011 1234 1234 1234
16 length, no space, start with 65 Credit Card#:6521123412341234
16 length, dashes, start with 65 Credit Card#:6521-1234-1234-1234
16 length, spaces, start with 65 Credit Card#:6523 1234 1234 1234
Negative (Should Not Fire):
16 length, no space, not start with 6011 6012123412341234
16 length, dashes, not start with 6011 6012-1234-1234-1234
16 length, spaces, not start with 6011 6012 1234 1234 1234
16 length, no space, not start with 65 6612123412341234
16 length, dashes, not start with 65 6612-1234-1234-1234
16 length, spaces, not start with 65 6612-1234-1234-1234
16 length, no space, start with 65, embedded Credit Card Number6521123412341234
16 length, dashes, start with 6011, malformed 601-11234-1234-1234
16 length num matching pattern but embedded User ID: 0000006011123412341234000000
`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", 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 Golang, please visit: https://golang.org/pkg/regexp/