s/// is an operator, so that means your variable contains Perl code. eval EXPR can be used to execute it.
But that's rather unsafe. I assume you don't want to allow arbitrary code execution (variable lookups, (?{}), (??{}) and /e). If so, you can use the following:
my ($pat, $repl, $mods) = $substitution =~ m{ ^s/ ( (?:[^/\\]+|\\.)* ) / ( .* ) / ( [a-z]* ) \z }xs or die("Bad substitution\n"); my %mods = map { $_ => 1 } $mods =~ /./sg; my $global = delete($mods{g}); $mods = join('', delete(@mods{qw( i m s x )})) and $pat = "(?$mods:$pat)"; die("Unknown modifiers \"", keys(%mods), "\"\n") if %mods; no re 'eval'; if ($global) { $candidate =~ s/$pat/$repl/g; } else { $candidate =~ s/$pat/$repl/; }
In reply to Re: Substitution with regexes in variables
by ikegami
in thread Substitution with regexes in variables
by nafion112
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |