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 | |
by davido (Cardinal) on Dec 06, 2006 at 07:25 UTC | |
|
Re^2: regex question
by Anonymous Monk on Dec 05, 2006 at 09:04 UTC | |
by davido (Cardinal) on Dec 05, 2006 at 09:10 UTC |