use strict;
my $str = '//Valid Values
D1
DF2,AB2
D1-2
A1, A2
D1 - 2
1 - FH15
1-R3
1,D2
1,H5,8,S4
1 , H5, 8, S4
1, D2, D6-8, 6-9
1,D2,D6-8,6-9,G2,8
1-D3,TT6-8
1-D3, TT6-8
1-3, VB8, 11-SD13, R15
//Invalid values
1*, 3
,1,F2
-1-TG3
12-D4,
D1-F2-
1- F2- 4
D10,/
1234s
F4-,8
4,-H5
D3-F-G7
D3 - F - G7
4 , - H5
1*,D
12 - D4';
my $regex = qr/^[A-Z0-9]+(?:-[A-Z0-9]+)?(?:,[A-Z0-9]+(?:-[A-Z0-9]+)?)*$/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