Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to have a regex to evaluate whether a user has entered a valid string. Valid strings are a comma delimited set of four digit numbers i.e 9500,9501,9502
I'd like to be able to know if a user has entered anything not confirming to that pattern i.e 9500,goats,9501 or 9500,,,,,,,,, that kind of thing.
Here's what I've got so far, it kinda sorta works but doesn't capture everything. Could anyone off any suggestions?
my $string = '9500,9501'; if ($string =~ /^[\d{4}\,]+$/) { print "Looks ok\n"; }else { print "Not ok\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Validating Comma Delimited String
by moritz (Cardinal) on Jan 08, 2009 at 23:31 UTC | |
|
Re: Validating Comma Delimited String
by gone2015 (Deacon) on Jan 08, 2009 at 23:37 UTC | |
|
Re: Validating Comma Delimited String
by zwon (Abbot) on Jan 08, 2009 at 23:32 UTC | |
|
Re: Validating Comma Delimited String
by Bloodnok (Vicar) on Jan 09, 2009 at 11:44 UTC |