Regular Expression for Integer and float values

Discussion in 'Perl Misc' started by Roop, Aug 17, 2006.

  1. Roop

    Roop Guest

    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
    Roop, Aug 17, 2006
    #1
    1. Advertising

  2. Roop

    Klaus Guest

    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?
    Klaus, Aug 17, 2006
    #2
    1. Advertising

  3. Roop

    Guest

    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
    , Aug 17, 2006
    #3
  4. Roop

    Dave Guest

    "Roop" <> wrote in message
    news:...
    > 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
    Dave, Aug 17, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page