use strict;
my $str = 'I can not find a working regular expression to match the strings followed by a number from 1 to 49 in braces, in a list dividing the pairs by comma:
alpha (1), beta (12), gamma (37), delta (49), epsilon (55), zeta (64)
but also in random order and amount of pairs like
zeta (64), alpha (1), gamma (37), beta (12), delta (49), epsilon (55), eta (48), theta (26)
and also random numbers like
zeta (11), gamma (71), beta (49), delta (52), epsilon (99), alpha (33)
I\'m looking for an endresult showing only the pairs with a number lower as 49 like
alpha (1), beta (12), gamma (37), delta (49)
alpha (1), gamma (37), beta (12), delta (49), eta (48), theta (26)
zeta (11), beta (49), alpha (33)';
my $regex = qr/\w+ \((?:[1-9]|[1234][0-9])\)(?:, )?/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