Re: Regular expression: How to determine wether entry is a number?
- From: mritty@xxxxxxxxx (Paul Lalli)
- Date: Wed, 30 Jul 2008 09:33:50 -0700 (PDT)
On Jul 29, 12:09 pm, janhorstm...@xxxxxx (Jan-Henrik) wrote:
On 29 juil, 17:40, mri...@xxxxxxxxx (Paul Lalli) wrote:
On Jul 29, 8:46 am, janhorstm...@xxxxxx (Jan-Henrik) wrote:
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...
Those two statements are a contradiction. One one hand, you say you
want the input to contain "small numbers with max of 4-5 digits."
Immediately, after that, you say "it's enough just to make sure the
input does not contain any letters". You seem to be operating under
the assumption that letters and numbers are the only possible inputs.
This is not true. As I replied to Aruna Goke elsewhere in this
thread, what do you expect to happen if someone gives you a string
containing "=-*&43# @8,>}" ?
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.
For example, if I read a string like this from a file:
-----------------------------------------
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...
You look for the number, using whichever regexp from my original post
is most appropriate, and "capture" it using parentheses.
A very quick example:
#!/usr/bin/perl
use strict;
use warnings;
my $string = "Cowmilk (l/h) 4.5023 Cows in
Europe 4.5"
if ($string = /(\d+(?:\.\d+)?)/) {
my $number = $1;
print $number;
} else {
print "No floating point number found in '$string'\n";
}
__END__
For more information, check out the following documents, by running
these commands at your command line:
perldoc perlretut
perldoc perlre
perldoc perlreref
Paul Lalli
.
- 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
- 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: Evaluate an operator given as a string
- Next by Date: use of Configuration files
- 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
|