Re: Regular expression: How to determine wether entry is a number?



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

.



Relevant Pages

  • Re: regular expression question
    ... > I am trying to test a string to see if it begins with a combinations of ... > 5 digits if don't know what the digits are going to be? ...
    (comp.lang.perl.misc)
  • Re: how to change string to scalar
    ... this is looking for the end string with digits. ... For additional commands, e-mail: beginners-help@xxxxxxxx ...
    (perl.beginners)
  • Re: How to convert extra long strings into their equivalent Hex Strings in VBA (Word 2K)
    ... numbers (upto 18 digits max) into its equivalent Hex String ... Public Function ExpressServiceCode(ByVal ServiceTag As String) As String ... 'the number dblTemp in the specified base, ... Dim lngTemp As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: BigNum -- Floating Point
    ... The 'N' is the number of decimal digits. ... The internal representation is really just a string of bits. ... the number of shifts for various multiples of ten: ... The 'exponent' is very closely related to ...
    (comp.programming)
  • Patterns in pi, copyright law, and philosophy
    ... Whether the offset of a string found in pi can be used as a form on ... then calculate how many digits of pi one would need to raise the ... "An infinite series of numbers is not an exhaustive set of numbers. ... finite string of digits occurs within the decimal expansion of pi, ...
    (sci.math)