in reply to [SOLVED] Evaluating user-entered captured groups during Perl substitution
Some quotes were wrong - use warnings would have told you. And an eval was in the wrong place.
EDIT: I shouldn't have posted this in a hurry. Sorry, this does not work.
At least for the given example this works:
#!/usr/bin/perl use strict; use warnings; #FROM DATABASE TEXT my $line = "Her house is on 34th Mt. Whitney St. near St. Mt. Helens S +t."; #FROM INCOMING FORM INPUTS my $query = '(St\.\s)(Mt\.\s)(?=Helens)'; my $substitution = 'Mount ${1}'; # USER MAY HAVE ENTERED "$1" #FOR RETURNED HTML HIGHLIGHT OF CHANGES my $start = q|<span class="highlight">|; my $end = "</span>"; return "Regex containing code disallowed." if $query =~ m[\(\?\??\{]; return "Regex containing code disallowed." if $substitution =~ m[\(\?\ +??\{]; my $replace = sub { my $evaluate = sub { return eval($1); }; my $val = $substitution; $val =~ s/(\$\{\d+\})/$evaluate->()/eg; $val = "$start$val$end"; return $val; }; eval "\$line =~ s/\$query/$replace->()/eg"; #EXPECTED TEXT AFTER SUBSTITUTION $line = "Her house is on 34th Mt. Whitney St. near Mount St. Helens St +.";
Greetings,
-jo
In Section
Seekers of Perl Wisdom