use strict;
my $str = 'work:
2022-01-01
01-01-2020
1990/01/01
12/30/2020
30/12/2020
30.12.2020
2020.12.30
19-12-30
30-12-19
2020/12/30
12/30/19
30/12/19
30.12.19
19.12.30
not work:
1022-01-01
01-15-2020
2990/01/01
13/30/2020
30/13/2020
30.12.9029
2020.14.30
2020.12.33
19-12-33
19-13-30
33-12-19
30-12-35
2020/13/30
2020/12/34
12/30/29
30/12/29
30.12.26
30.12.30
13/30/19
30/13/19
30.13.19
19.13.30
12/33/19
33/12/19
33.12.19
19.12.33
1/1/19
1/01/19
01/1/19
1/01/2019
1/1/2019
01/1/2019
';
my $regex = qr/(((20[012]\d|19\d\d)|(1\d|2[0123]))-((0[0-9])|(1[012]))-((0[1-9])|([12][0-9])|(3[01])))|(((0[1-9])|([12][0-9])|(3[01]))-((0[0-9])|(1[012]))-((20[012]\d|19\d\d)|(1\d|2[0123])))|(((20[012]\d|19\d\d)|(1\d|2[0123]))\/((0[0-9])|(1[012]))\/((0[1-9])|([12][0-9])|(3[01])))|(((0[0-9])|(1[012]))\/((0[1-9])|([12][0-9])|(3[01]))\/((20[012]\d|19\d\d)|(1\d|2[0123])))|(((0[1-9])|([12][0-9])|(3[01]))\/((0[0-9])|(1[012]))\/((20[012]\d|19\d\d)|(1\d|2[0123])))|(((0[1-9])|([12][0-9])|(3[01]))\.((0[0-9])|(1[012]))\.((20[012]\d|19\d\d)|(1\d|2[0123])))|(((20[012]\d|19\d\d)|(1\d|2[0123]))\.((0[0-9])|(1[012]))\.((0[1-9])|([12][0-9])|(3[01])))/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