#!/usr/bin/perl use strict; use warnings; my $text = '1 + 3 = [532] {hallo}'; my $string = q~+?.*^$()[]{}|\\~; $text =~ s/([\Q$string\E])/\\$1/g; print $text;