Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Filtering unwanted chars from input field

by Anonymous Monk
on Dec 18, 2012 at 14:01 UTC ( #1009369=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Filtering unwanted chars from input field
in thread Filtering unwanted chars from input field

Would I be breaking the law by allowing a "-" and updating the code to:

sub filter { my $str = shift; defined $str or return ''; $str =~tr{A-Za-z0-9_.-}{}cd; return $str; }


Comment on Re^3: Filtering unwanted chars from input field
Download Code
Re^4: Filtering unwanted chars from input field
by johngg (Abbot) on Dec 18, 2012 at 14:14 UTC

    That will work ok.

    $ perl -E ' > @data = ( > q{Hello, world!}, > q{te-st%$/*.}, > q{All_of_this_is_OK.}, > q{!@#$%^&*()12345}, > ); > say for map { tr{A-Za-z0-9_.-}{}cd; $_ } @data;' Helloworld te-st. All_of_this_is_OK. 12345 $

    Cheers,

    JohnGG

      What is I want to allow spaces as well? \s+ does not work.

        The left hand side of the tr is not a regular expression. Just include a real space in the character list.

        $ perl -E ' > @data = ( > q{Hello, world!}, > q{te-st%$/*.}, > q{All_of_this_is_OK.}, > q{!@#$%^&*()12345}, > ); > say for map { tr{A-Za-z0-9_. -}{}cd; $_ } @data;' Hello world te-st. All_of_this_is_OK. 12345 $

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009369]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-09-23 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (219 votes), past polls