use strict;
my $str = '4/5"
4 m/s
4/3m/s
4/3" in ms
4/3km/h
4/3km/h in mph
4m in cm
9.0m in cm
9,0m in cm
9,0m>cm
9,0m>cm
9,0 m>cm
9,0 m > cm
9 meters as centimeters
1 m^3
4$>€
4€
4$>¥
# Test queries from https://phabricator.kde.org/D22869
3/8"
3/8 in
3/8" in cm
3/0"
3"
3€>₱
# Should not match
4 in m/s
4in m/s
4">';
my $regex = qr`^(?P<queryValue>[0-9,./]+) ?(?P<sourceUnit>[a-zA-Z/\"'^0-9\p{Sc}]+)(?:(?: in | to | as | ?> ?)(?P<targetUnit>[a-zA-Z/\"'^0-9\p{Sc}]+))?$`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