Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Inverse regexes for input validation

by sgifford (Prior)
on Mar 28, 2007 at 16:45 UTC ( [id://607064]=note: print w/replies, xml ) Need Help??


in reply to Inverse regexes for input validation

The problem is the ^ "beginning-of-string" anchor at the beginning of your regex, and the $ "end-of-string" anchor at the end. It ends up meaning "if the entire string consists of invalid characters, delete the entire string". What I think you mean is "if there are any invalid characters in the string, replace them with nothing", which you get by simply removing the anchors:
$test1 =~ s/[^A-Za-z\-\ ]+//g;

By the way, if you're dealing with untrusted input and trying to avoid doing unsafe things with it, you should read about "taint mode" in perlsec.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found