use strict; use warnings; my $snt1 = 'this.is.example.text'; my $snt2 = '[this.is.] [example.text]'; foreach ($snt2 =~ /\[(.+?)\]/g) { $snt1 =~ s/\Q$_\E/[$_]/; } print $snt1;