$_ = "the\\/bad\$dataStuff"; # your data of course :) $OK_CHARS='-a-zA-Z0-9_.@'; # allowed characters s/[^$OK_CHARS]/_/go; # replace invalid chars with _ $user_data = $_; # sanitized version print $user_data; # output: the__bad_dataStuff