/usr/bin/perl -w use strict; my $text= "start \\chapter{hello}\n end\n"; my $inpattern = qr/\\chapter\{(.*)\}/; my $outpattern = " -- FOUND \$1 -- "; $text =~ s/$inpattern/$outpattern/ge; print "$text\n";