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


In reply to Re: regex question by davido
in thread regex question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.