in reply to simple regular expression

Does the following come close to what you are looking for? I've modified your snippet so that a pattern is actually read from a file. Putting the s/// operation inside a string eval is an effective way to do what you want (my version does have "FOUND hello" in its output ). Note the backslash before "$text", so that this variable does not get interpolated into the string before it is evaluated as code:
#!/usr/bin/perl use strict; use warnings; my $text = "start \\chapter{hello}\n end\n"; my $patternreadfromfile = <DATA>; chomp $patternreadfromfile; my $inpattern = qr/\\chapter\{(.*)\}/; eval "\$text =~ s/$inpattern/$patternreadfromfile/g"; print "$text\n"; __DATA__ -- FOUND $1 --

(update: sorry, this node should have been a reply to your other node in this thread, at Re^2: simple regular expression)