Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Need a regex..

by littlemonk (Sexton)
on May 21, 2012 at 13:01 UTC ( #971607=perlquestion: print w/replies, xml ) Need Help??
littlemonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I need a regex for validating user input should accept any digit before and after decimal for example 12.12, 123.123,0.1, etc an it should not accept more than 3 digits before and after decimal example 1234.0 ,1234,.etc .. help me ..thanks in advance.

Replies are listed 'Best First'.
Re: Need a regex..
by AnomalousMonk (Canon) on May 21, 2012 at 14:33 UTC

    You don't say what should happen with negative numbers, but it's easy enough to exclude them in the same way four-or-more digit patterns are excluded.

    Update: A link to Regexp::Common might be helpful.

    >perl -wMstrict -le "use Regexp::Common; ;; for my $t (qw( 12.12 123.123 1 12 123 1. 12. 123. .1 .12 .123 0.1 1.0 0.0 1234.0 1234 0.1234 .1234 -123 -.12 -0.1 )) { my $ok = $t =~ m{ \A (?! .* \d{4}) $RE{num}{real} \z }xms; printf qq{%s '$t' \n}, $ok ? 'accept' : 'REJECT'; } " accept '12.12' accept '123.123' accept '1' accept '12' accept '123' accept '1.' accept '12.' accept '123.' accept '.1' accept '.12' accept '.123' accept '0.1' accept '1.0' accept '0.0' REJECT '1234.0' REJECT '1234' REJECT '0.1234' REJECT '.1234' accept '-123' accept '-.12' accept '-0.1'
Re: Need a regex..
by Neighbour (Friar) on May 21, 2012 at 13:34 UTC
    Could you specify whether only one number is entered per line, or if multiple numbers are entered per line, how are they separated?

    You can use {n,m} after any matching character/group to specify how often it is allowed to match (minimal n, maximal m). See also QUANTIFIERS.
Re: Need a regex..
by scorpio17 (Abbot) on May 21, 2012 at 13:35 UTC
    Something like this, maybe?
    use strict; while (my $data = <DATA>) { chomp $data; if ($data =~ /^\d?\d?\d\.\d\d?\d?$/) { print "GOOD\n"; } else { print "BAD\n"; } } __DATA__ 12.12 123.123 0.1 1234.0 1234
      it should match non-decimal value also ...example 12,123, and it should not match more than 3 digits before and after decimal..i have one regex(/^\- \+{0,1}0-9{0,3}(\.){0,1}0-9{0,3}\%{0,1}$/) but its accepting 1234(more than 3 digits before decimal) ... plz help me

        Please wrap code it <code> tags to keep things legible. As you can see, your character classes got linkified.

        1234 is passing because it's being read as (123)(4). You should set the whole part following and including the decimal in a non-capturing group with a conditional: /^[-+]?[0-9]{0,3}(?:\.[0-9]{0,3})?%?$/ I've also removed a bunch of unnecessary escaping, and changed your {0,1} to the shorter ? synonym.

        A read through of perlretut may be helpful.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Edit: Lost the race to Re: Need a regex.., which is also slightly better :)
        Try a more grouped approach:
        First the start of the line: ^
        then an optional + or - sign, with the + enclosed in [] to avoid interpretation: ([+]|-)?
        then 0-3 digits: [0-9]{0,3}
        then an optional group of a period followed by 1-3 digits, with the period escaped in another way to avoid interpretation: (\.[0-9]{1,3})?
        and finally the end of the line: $
Re: Need a regex..
by diddy_perl (Novice) on May 21, 2012 at 14:38 UTC

    Just slightly modified the previous post:

    use strict; while (defined(my $data = <DATA>)) { chomp $data; print "Got: ", $data, "\n"; if ($data =~ /[-+]?\d+| # optional sign followed by whole number or [-+]?\d{0,3} # optional sign followed by at most 3 digits (\.\d{1, 3})? # followed by decimal point, and finally by at mo +st 3 digits /x) { print "GOOD\n"; } else { print "BAD\n"; } } __DATA__ 12.12 123.123 0.1 1234.0 1234

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://971607]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2017-01-16 21:47 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (151 votes). Check out past polls.