use strict;
my $str = 'Should match:
1 Cups
2 tsp
3 tbps
1/2 tablespoon
3 tablespoons
1 tablespoon
3 teaspoons
1 small
Small chiles
23 Small chiles
1/2 a Small chile
1 1/2 cups kewpie
11/2 cups kewpie
1/3 cup fresh lemon juice
1 medium onion
Large
medium
Freshly
1½ cups Kewpie
½ cups Kewpie
1 pint
4 ounces
1 pound
2 pounds
Should not match:
1 Make the sauce
1 toast the bread
I want to make 1 piece of toast';
my $regex = qr/(^([0-9[:punct:]0-9]+|\p{No}|\d\p{No}) ?\s* ?([0-9[:punct:]0-9]+|\p{No}|\d\p{No})* ?\w* (cup|tsp|tbps|tablespoon|teaspoon|small|medium|pint|ounce|pound))|(^(small|Large|medium|Fresh))/mip;
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