$string =~ $regexp works if $regexp contains a regular expression. It doesn't in your case. "s/.../.../" is the Perl substitution operator, not a regular expression.
If given strings, the best way I can think of doing a substitution is:
$regexp = "a"; $subst = "b"; $global = 1; if ($global) { $string =~ s/$regexp/$subst/g; } else { $string =~ s/$regexp/$subst/; } # (?i...) can be used in $regexp for /.../i # (?m...) can be used in $regexp for /.../m # (?s...) can be used in $regexp for /.../s # (?x...) can be used in $regexp for /.../x # /g cannot be stored in $regexp, thus the need for $global. # /e cannot be stored in $regexp.
In reply to Re: Exec'ing a regex stored in a scalar variable
by ikegami
in thread Exec'ing a regex stored in a scalar variable
by brycen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |