Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular Expression for Integer and float values

Reply
Thread Tools

Regular Expression for Integer and float values

 
 
Roop
Guest
Posts: n/a
 
      08-17-2006
Hello All

I want to ask regading regular expression.
I want to use such regular expression which only allow integers and
float value.
for example :--

12
12.34
23.456
0.5
0.0
0

I found but not able to find. Please Help me . Suggest me the regular
expression for that


with regards
Tarun sinha

 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      08-17-2006
Roop wrote:
> I want to ask regading regular expression.
> I want to use such regular expression which only allow integers and
> float value.


see Perlfaq 4:
How do I determine whether a scalar is a number/whole/integer/float?

 
Reply With Quote
 
 
 
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      08-17-2006
Roop <> wrote:

> I want to ask regading regular expression.
> I want to use such regular expression which only allow integers and
> float value.
> for example :--


> 12
> 12.34
> 23.456
> 0.5
> 0.0
> 0


> I found but not able to find. Please Help me . Suggest me the regular
> expression for that


Lets take this step by step... based on your examples...

m/^\d+ # At least one digit (and nothing else) to start the value
(?: # Start a cluster but don't capture
\.\d+ # We may have a decimal point followed by at least one digit
)? # The decimal point + following digits may occur either
# once or not at all
$ # Nothing else can follow
/x; # And allow whitespace and comments in the RE

This allows all your examples, but not for example: 5.
as it does not have a following digit.

Axel
 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      08-17-2006

"Roop" <> wrote in message
news: ups.com...
> Hello All
>
> I want to ask regading regular expression.
> I want to use such regular expression which only allow integers and
> float value.
> for example :--
>
> 12
> 12.34
> 23.456
> 0.5
> 0.0
> 0
>
> I found but not able to find. Please Help me . Suggest me the regular
> expression for that
>
>
> with regards
> Tarun sinha
>


Look at the cpan module Regexp::Common. Better than re-inventing the wheel
unless you have unusual constraints.

Something like:
use Regexp::Common;
my $NUMBER = $RE{num}{real}{-keep};
# later
my ($number) = $input =~ $NUMBER;

but read the docs to get just what you want.

Advise based on Perl Best Practices p263




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Regular Expression for both integer and decimal values GD ASP .Net 2 09-30-2004 02:15 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57