Sorry, thought it sounded simpler than it was. :)
However I did do something similar myself not too long ago for a program I wrote. This was for a regular expression tutor I wrote ( which I hope to post to craft as soon as I iron out one blasted bug ) so hopefully this will prove useful to you.
=head2 If we've had a regular expression with grouping, we need to sho
+w the
matches we made there too. Unless we count the number of matchi
+ng
parens, which could get odd if there was a match looking for '(
+' and ')',
we run a simple counter and check if the match variable exists.
+ If it
does, we print it out. If no parenthesis are used, $match_resul
+ts holds
our match.
=cut
{eval "\@matches = (\$user_text =~ $regex); \$match_results .=
+\$&;";
$match_results .= "\n";
$match_results .= " Variables:\n"if defined($matches[$i])
+;
for (my $i = 1; $i <= @matches; $i++) {
my $found = $matches[$i - 1];
$match_results .= " \$$i:$found \n" if defined($matc
+hes[$i]);
}
} # end eval block
} # end if substr eq 'm'
This does encompass multiple matches on an m//. The s// and tr// I did like: eval "\$match_results =~ $regex
Of course I did have the help of some here to pull this stunt off :)
Good luck! |