#!/usr/bin/perl -w $string = "Test: FileNames can't have / \\ and other special characters"; $string =~ s/(.*):(.*)\/(.*)\\(.*)/$1COLON$2FSLASH$3BSLASH$4/; # Note greedy substituion in place print $string,$/; my %subs = qw(: COLON / FSLASH \\\\ BSLASH); $string =~ s/$_/$subs{$_}/ for (keys %subs); print $string,$/; #### TestCOLON FileNames can't have FSLASH BSLASH and other special characters TestCOLON FileNames can't have FSLASH BSLASH and other special characters