use strict;
my $str = '01/04/1999
31/03/1999
29/02/2000
30/02/2000
28/2/2001
29/2/2001
30/4/2002
31/4/2002
31/1/2003
32/1/2003
31/3/2004
01/4/2004';
my $regex = qr/^(?:
# Apr - Dec, 1999
(?:(?:(?:0?\d|[12]\d|3[01])\/(?:0?[578]|1[02]))|(?:(?:0?\d|[12]\d|30)\/(?:0?[469]|11))|(?:(?:0?\d|1\d|2[0-8])\/0?2))\/1999
|
# 2000 - 2003
(?:(?:(?:0?\d|[12]\d|3[01])\/(?:0?[13578]|1[02]))|(?:(?:0?\d|[12]\d|30)\/(?:0?[469]|11))|(?:(?:0?\d|1\d|2[0-8])\/0?2))\/200[0-3]
|
# Jan - Mar, 2004
(?:(?:(?:0?\d|[12]\d|3[01])\/(?:0?[13]))|(?:(?:0?\d|1\d|2[0-8])\/0?2))\/2004
|
# Leap years!
29\/0?2\/200[04]
)$/mxp;
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