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

...and probably is. I'm trying to filter out any non-(alphanumeric or whitespace) characters from a string.
$string !~ s/\w|\S//g;
(or anything like it that I tried) doesn't work and the few tangles with look-aheads have proven unsuccessful.

Replies are listed 'Best First'.
RE: It seems simple...
by Anonymous Monk on Feb 25, 2000 at 13:23 UTC
    by this you mean: "ab&^#c( 1 2"=>"abc12"? if so try $string=~s/\W//g; not sure what you meant, the $var!~s///g syntax seems weird to me.
Re: It seems simple...
by Anonymous Monk on Feb 24, 2000 at 09:21 UTC
    The !~ operator negates the return value of the =~ operator, not of the regular expression. In an s/// operation, the result of $string will be the same regardless of whether !~ or =~ is used, as all that is being negated is the number of matches returned. What you're looking for is $string =~ s/\W|\s//g;
      Unfortionatly, that doesn't work.
      s/\W|\s//g;
      successfully does what it says it will - both:
      s/\W//g;
      and
      s/\s//g;
      thereby eliminating the whitespace that I wanted to preserve (both regular expressions do that, actually)
        Sorry, I misread your question the first time (me having posted the first answer).. I think what you want is $string =~ s/[^\w\s]//g;