in reply to Regexp substitution using variables
#!/usr/bin/perl use warnings; use strict; my $string = 'abc'; my $pattern = 'B'; my $replacement = 'X'; my $flags = 'i'; $string =~ s/(?$flags:$pattern)/$replacement/; print $string; # aXc
But you can't do that for /gore.
Update: Even string eval doesn't help, as plain interpolation of the $replacement can break if it contains a slash.
doesn't work either, as you can't put $1 into $replacement unless you always use /ee which makes it unsafe again.eval "s/\$pattern/\$replacement/$flags"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regexp substitution using variables
by MikeTaylor (Sexton) on Nov 25, 2020 at 22:24 UTC | |
by LanX (Saint) on Nov 26, 2020 at 00:40 UTC | |
by bart (Canon) on Nov 28, 2020 at 11:49 UTC | |
by MikeTaylor (Sexton) on Nov 25, 2020 at 22:36 UTC | |
by LanX (Saint) on Nov 25, 2020 at 23:56 UTC | |
by MikeTaylor (Sexton) on Nov 26, 2020 at 10:34 UTC | |
by Bod (Parson) on Nov 25, 2020 at 22:32 UTC | |
by MikeTaylor (Sexton) on Nov 25, 2020 at 22:48 UTC | |
|
Re^2: Regexp substitution using variables
by Bod (Parson) on Nov 25, 2020 at 22:03 UTC |