Regular Expression for Integer and float values

R

Roop

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
 
K

Klaus

Roop said:
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?
 
A

axel

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

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
 
D

Dave

Roop said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top