Re: Regular expression: How to determine wether entry is a number?
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Wed, 30 Jul 2008 14:50:49 +0100
Jan-Henrik wrote:
On 29 juil, 14:46, janhorstm...@xxxxxx (Jan-Henrik) wrote:
Dear Group,
I'm new to Perl and I have a simple question:
I ask for the entry of a number vie <STDIN>:
----------------------------------------
#!/usr/bin/perl -w
use strict;
my $foo;
print "Enter number: ";
$foo = <STDIN>;
comp($foo);
----------------------------------------
Now I would like to check wether the user really entered a number and
not letters. What would a check like that look like? A regular
expression like this:
----------------------------------------
unless ($foo =~ /[a-zA-Z\D+][^.][\D*]/ {...};
----------------------------------------
Is there an easier or more beautiful way?
Also, how would I substract just a number from a string? Searched the
net for an example but didn't succeed, so sorry for asking a question
like that...
Many thanks for your help!
Kind regards,
Jan-Henrik
I'm stupid, sorry.
It's as easy as this:
----------------------------------------
unless ($foo =~ /[a-zA-Z]/ {...};
----------------------------------------
:-)
No, it's not.
All the punctuation passes that test as Paul has pointed out, and do you want to
stop them from entering 1E6 fpor one million?
If you're after purely numeric input you can (after chomping the input) write
unless ($input =~ /\D/) {
:
}
but it's still a bit draconian to disallow leading and trailing whitespace.
Rob
.
- References:
- Regular expression: How to determine wether entry is a number?
- From: Jan-Henrik
- Re: Regular expression: How to determine wether entry is a number?
- From: Jan-Henrik
- Regular expression: How to determine wether entry is a number?
- Prev by Date: Re: reading a few bytes into a file
- Next by Date: Re: How to get a computed string to act as a re in if statement
- Previous by thread: Re: Regular expression: How to determine wether entry is a number?
- Next by thread: Re: Regular expression: How to determine wether entry is a number?
- Index(es):
Relevant Pages
|