my $string = 'fooXbarXbazXboff'; my $separator = 'X'; my $replace = ''; $string =~ s{ \Q$separator\E }{$replace}xmsg; dd $string;