keesturam has asked for the wisdom of the Perl Monks concerning the following question:
I am practicing few Regex exercises, one was this,
Use regular expressions (RE) to check that the data you get when asking for numbers, are actually numbers. Also check that the operation is valid.
These should all be considered as numbers: "4" "-7" "0.656" "-67.35555"
These are not numbers: "5." "56F" ".32" "-.04"
I have come up with this code, although I know it is not the best way. Can you suggest a better way to search in a single go rather than using OR?
print ("Enter the number to check if it is actually a number:"); $input = <STDIN>; while ($input ne "") { chop ($input); if ($input =~ /^\d+$/ | $input =~ /^\d+.\d+$/ | $input =~ /^-\d+$/ | $ +input =~ /^-\d+.\d+$/) { print ("Yes, it is a number\n"); } else { print ("No, it is not a number\n"); } print ("Give another input:"); $input = <STDIN>; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Reg Ex exercise
by choroba (Cardinal) on Dec 05, 2012 at 16:28 UTC | |
Re: Reg Ex exercise
by toolic (Bishop) on Dec 05, 2012 at 16:44 UTC | |
Re: Reg Ex exercise
by thundergnat (Deacon) on Dec 05, 2012 at 16:26 UTC | |
Re: Reg Ex exercise
by 2teez (Vicar) on Dec 05, 2012 at 16:28 UTC | |
Re: Reg Ex exercise
by Rudolf (Pilgrim) on Dec 05, 2012 at 16:18 UTC | |
by thundergnat (Deacon) on Dec 05, 2012 at 16:49 UTC | |
by keesturam (Initiate) on Dec 05, 2012 at 16:27 UTC | |
by Rudolf (Pilgrim) on Dec 05, 2012 at 16:40 UTC | |
Re: Reg Ex exercise
by BillKSmith (Monsignor) on Dec 05, 2012 at 18:08 UTC | |
Re: Reg Ex exercise
by Athanasius (Archbishop) on Dec 05, 2012 at 16:28 UTC | |
Re: Reg Ex exercise
by grondilu (Friar) on Dec 05, 2012 at 16:29 UTC |
Back to
Seekers of Perl Wisdom