Hi Monks,
I'm currently trying to write some perl to:
read input
validate that the input is alpha-numeric, a hyphen or a space
strip out "malicious characters"
return the sanitised input
Whilst I seem to be able to match any input that is not "safe", I'm having problems with the substitution. After playing around with it on and off for the last 2 days, I'm not managing to nail it. Can anyone tell me what I'm missing please?
(code is part of a larger script, hence the sub, I'm about 3 days into perl so apologies if my code/layout sucks!)
Thanks in advance.
#!/usr/bin/perl
#supply test strings on the command line for ease
$test1 = shift(@ARGV);
sub validate_text
{
if ( $test1 =~ m/^[A-Za-z0-9\-\ ]+$/ )
{
print "Text clean\n";
return ($test1);
} else {
$errorstring = "Funny business with variables occuring
+, have attempted to fix";
print "$errorstring\n";
print "dollartest before fixing = $test1\n";
$test1 =~ s/^[^A-Za-z\-\ ]+$//g;
print "dollartest after fixing = $test1\n";
return ($test1);
}
}
$validated = &validate_text;
print "the validated text is: $validated\n";
The output from the script follows:
[lowprivuser@localhost testing]$ perl regexp-check2.pl abcd\"efg
Funny business with variables occuring, have attempted to fix
dollartest before fixing = abcd"efg
dollartest after fixing = abcd"efg
the validated text is: abcd"efg
[lowprivuser@localhost testing]$ perl regexp-check2.pl abcdefg1
Text clean
the validated text is: abcdefg1
[lowprivuser@localhost testing]$
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.