Monday, 15 March 2010

regex - Perl: Remove non-letter, non-number characters from string -


There is a new problem in Perl, I'm hoping someone can help me.

I is a Perl string that is allowed to keep letters from A to Z (capital and lowercase), numbers 0 to 9, and "-" and "_" characters. I use all non- I want to remove matching characters, so that the rest should not be left untouched. So "Hello @ _World" will be "Hell_World" I know that doing this is probably an easy, easy way to do this but I'm new to Pearl and do not know how to approach this. Any help would be greatly appreciated.

You will use replace with ^ (not) while regular expression Pearl provides shortcuts, you can see parts more clearly:

$ string = ~ s / [^ [alnum:] _-] // g;

Where [: alnum:] is a character square for alphabet and numeric characters


No comments:

Post a Comment