$strvar = "test text"; $pattern = "\$strvar =~ s/(text)/\\[tag]$1\[/tag]/ig"; eval $pattern;