in reply to regex question

Use tr///

$string =~ tr/A-Za-z0-9.-_//cd;

The /c modifier "complements" the terms listed. That means that instead of replacing A-Z with nothing, it will replace the complement of A-Z (which is every character that is NOT A-Z). We've specified A-Z, as well as your other ranges and special characters. The /d modifier means delete any listed character (or in this case complement to the listed characters) that is not mirrored on the right hand side of the operator. Since we leave the right hand side empty, everything not matching our criteria will be deleted. This will delete any character that is not A-Z a-z, 0-9, ., -, and _. It's also possible (and easy) with the s/// operator like this:

$string =~ s/[^A-Za-z0-9._-]//g;

This works a little differently: It substitutes any character not found in the character class with nothing (which means to delete that character). The /g modifier causes s/// to iterate through every match.

You could have a look at perlop to better understand tr///, and perlre for help with the regular expression. It's important to note that even though it looks kind of like a regular expression, the transliteration operator (tr///) is not a regexp.


Dave

Replies are listed 'Best First'.
Re^2: regex question
by bart (Canon) on Dec 06, 2006 at 07:09 UTC
    $string =~ tr/A-Za-z0-9.-_//cd;
    You forgot to escape the hyphen, the one you want taken literally:
    $string =~ tr/A-Za-z0-9.\-_//cd;

    Yours will keep everything in the range

    ./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
    too, which is more than you wanted.

      What I should have said (and intended to but didn't) is:

      tr/A-Za-z0-9._-//cd;

      The hyphen doesn't have special meaning in that case. I even did that when I composed the character class demonstrated in my s/// example, but somehow missed it in the tr/// example.

      Good catch bart. :) You've got to love Perl's density huh? ;)


      Dave

Re^2: regex question
by Anonymous Monk on Dec 05, 2006 at 09:04 UTC

    Thanks for your replay , but ...

    #!/perl/bin/perl -w use strict; my $string = "Hello $ World \n"; $string =~ tr/A-Za-z0-9.-_//cd; print $string; ### ### prints out Global symbol "$World" requires explicit package name

    same for the other example

    and i have one more question how can i add a space

    to s/// in the example you gave to me ?

    leaving a space at the end or adding \s giving me errors

      Yes, but if you used the correct type of quotes that wouldn't be a problem:

      my $string = 'Hello $World' . "\n";

      Or escape your $ character.

      By the way, your script is failing on this line:

      my $string = "Hello $World \n";

      That's happening in the compilation phase, not runtime.


      Dave