use strict;
my $str = '2min baz
12 mins foo
1 hour 20 mins remind this
1sec
5 hours
1 sec foo
3 minutes 4 seconds
3333 sec do
1 hrs 20 mins 5 seconds remind this
2 days 3 hrs 20 mins 5 seconds remind this
foo 2sec bar //fail
2 s hi
3 m foo
3m bar
2d baz
9 h foobar';
my $regex = qr/(?(DEFINE)
(?<int>
(?:
\G|
(?!\n)
)
\s*\b\d{1,5}\s*
)
(?<timepart>
(
s|
m|
h|
d
)
\b
)
)
^
((?&int))
((?&timepart))
(?<string>.+)
$/ximp;
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