$ perl -le ' > $str = q{aaabbbaaa}; > $repl = q{$1}; > $str =~ s{a+(b+)a+}{$repl}ee; > print $str;' bbb $