Re: Regular expression: How to determine wether entry is a number?
- From: janhorstmann@xxxxxx (Jan-Henrik)
- Date: Tue, 29 Jul 2008 09:09:38 -0700 (PDT)
On 29 juil, 17:40, mri...@xxxxxxxxx (Paul Lalli) wrote:
On Jul 29, 8:46 am, janhorstm...@xxxxxx (Jan-Henrik) wrote:
I'm new to Perl and I have a simple question:
Yes, but unfortunately it doesn't have a simple answer.... :-/
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);
I think you meant "chomp", not "comp".
----------------------------------------
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?
Well the problem is that you need to define what you mean by "a
number". Depending on what kinds of numbers are valid, your regexp
might be simple or very complex:
A single digit: /^\d$/
A whole number: /^\d+$/
An integer, possibly negative: /^-?\d+$/
A floating point number, possibly negative: /^-?\d+\.\d+$/
An integer *or* floating point number, possibly negative: /^-?\d+(?:\.
\d+)?$/
A number in scientific notation: /^-?\d+(?:\.\d+)?(?:[eE]-?\d+(?:\.\d
+)?)?$/
There are more complications that could be thrown in too. Like, is a
+ sign allowed for positive numbers? Do floating points that are less
than 0 have to start with 0, or can just ".9" be a valid number? Can
they use the comma to separate thousands?
Depending on what you're looking for, you might be better off using
the Regexp::Common::number module, found on the CPAN athttp://search.cpan.org/~abigail/Regexp-Common-2.122/lib/Regexp/Common...
Thank you very much for the detailed answer, I'll use it as a
reference in the future.
My case today: I just want to enter small numbers with a max of 4-5
digits or so, nothing special. So it's enough to just make sure the
input does not contain any letters...
For example, if I read a string like this from a file: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...
I don't know what you're trying to ask here. Please give us an
example of what you want to do - what the string will contain before
you do something to it, and what the string will look like afterwards.
Good luck,
Paul Lalli
-----------------------------------------
Cowmilk (l/h) 4.5023 Cows in Europe 4.5
-----------------------------------------
I want to "extract" (sorry for using substract in the first post) the
4.5023 from the string, how do I do that in a smart way? I need to do
so simple calculations with that number... And the position in the
string is not always the same, neither is the number of digits...
Again, thank you very much for you help, I really appreciate it.
Jan-Henrik
.
- Follow-Ups:
- Re: Regular expression: How to determine wether entry is a number?
- From: Paul Lalli
- Re: Regular expression: How to determine wether entry is a number?
- 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: Paul Lalli
- Regular expression: How to determine wether entry is a number?
- Prev by Date: Re: Regular expression: How to determine wether entry is a number?
- Next by Date: Re: getting process id under NT
- 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
|