use strict;
my $str = '4242424242424242
4012888888881881
4000056655665556
5555555555554444
5200828282828210
5105105105105100
378282246310005
371449635398431
6011111111111117
6011000990139424
30569309025904
38520000023237
3530111333300000
3566002020360505
8942424242424242
63942424242
445299295665920
Visa
Visa
Visa (debit)
MasterCard
MasterCard (debit)
MasterCard (prepaid)
American Express
American Express
Discover
Discover
Diners Club
Diners Club
JCB
JCB
Invalid
Invalid
Invalid';
my $regex = qr/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html