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

Hello Wise Monks,

I want to turn into spaces any non-letter, non-number and non-space characters from a string. The following code doesn't seem to work:

$stringtoclean=~s/[^A-Za-z0-9 ]+/ /
How should I write this so that it'll work?

Thanks in advance.

Gorby

Replies are listed 'Best First'.
Re: Removing unwanted characters
by ambrus (Abbot) on Oct 18, 2005 at 13:12 UTC

    Add a g flag. Like $stringtoclean=~s/[^A-Za-z0-9 ]+/ /g

Re: Removing unwanted characters
by samizdat (Vicar) on Oct 18, 2005 at 13:21 UTC
    s/[^A-Za-z0-9]/ /g;
    _each_ unwanted character will be replaced by a single space. Is this what you want? Your regex would replace one _group_ of unwanted characters with a space.
Re: Removing unwanted characters
by Fletch (Bishop) on Oct 18, 2005 at 13:52 UTC

    And of course TMTOWTDI: y/A-Za-z0-9 / /c;

      And by adding the s flag, any run of such characters will translate into a single space.

      Caution: Contents may have been coded under pressure.