qq wisely pointed out a problem with a validation
script I am writing for untainting purposes. The value *must* contain at least one number, but *may* also contain periods and hyphens. I *solved* it, but it look's messy. Can it be written more concisely?
#!/usr/bin/perl -w
use strict;
my $number = "..2314-1234123.";
if ($number =~ /^((?:[0-9\.-]*)(?:[0-9]+)(?:[0-9\.-]*))$/) {
print "$1\n";
}
Added after posting In summary, it's a regular expression that asks for at least one of a character class, but can also have some from another class.
Thanks, monks.
Update: Thanks all for participating. This was a great learning experience. If Tiger Woods were only a Perlmonk..... It seems like there is Perl and then there are regular expressions. So far
Abigail-II has the lead with 16 and a reg ex that even I can understand. Now, about these 2000 lines of code I have....
Update 2: I spoke too soon, I believe
Abigail-II's 2 solutions need the capturing parens to return $1--so the race is a bit tighter.
—Brad
"A little yeast leavens the whole dough."
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.