chucka6 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to strip all single quotes, double quotes, and newline characters from a string. What is the code to do this? Thanks in advance!

Replies are listed 'Best First'.
Re: Strip bad characters from a string
by chromatic (Archbishop) on Apr 06, 2000 at 23:12 UTC
    You want tr///, as in: $user_input =~ tr/"'\n//d; Be sure to use the /d at the end, as this tells the tr operator to Delete the characters specified.

    s/// would also work, but it is quite a bit faster to use tr///.

Re: Strip bad characters from a string
by btrott (Parson) on Apr 06, 2000 at 23:12 UTC
    You want tr.
    $_ = <<STOP; 'foo' is in single quotes, "bar" is in double quotes, and there are carriage returns between STOP tr/'"\n//d; print;
      Instead of just stripping them, how do I make a ' into a \' and " into a \". I tried using your example and adapting it but the result was \\\\\\\\\\\\\\\\\\\\\\\\\\\\' because it keeps looping just looking for the single character. Any ideas appreciated. Ian.
        Try
        s#(['"])#\\$1#g;
        Result (using the same string that I used the first time):
        \'foo\' is in single quotes, \"bar\" is in double quotes, and there are carriage returns between
Re: Strip bad characters from a string
by turnstep (Parson) on Apr 11, 2000 at 22:09 UTC
    s/(['"])/\\$1/g;
      I have
      $a = "dfowusgfiwu42394353****hsdfsd"; print "$a\n"; $a =~ s/!([a-zA-Z0-9])//g; print "$a\n";
      what's not right? it doesn't do anything to the string... and I know it looks useless, but it's a test for something else. what I want is to delete any and all characters from a string that don't fit into [a-zA-Z0-9]... unless things like "$(@$&%*" are in there...