Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Regex for max length float

by Sidhekin (Priest)
on Mar 14, 2007 at 14:55 UTC ( [id://604830]=note: print w/replies, xml ) Need Help??


in reply to Regex for max length float

I'm not sure what you mean with "precision" (why isn't "12.3" valid?), but judging by your examples, and assuming your "number" really is a string, I come up with this:

$float =~ /\A(?:\d|\d?\.\d{1,3})\z/;

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Replies are listed 'Best First'.
Re^2: Regex for max length float
by agianni (Hermit) on Mar 14, 2007 at 15:29 UTC

    Of course! I forgot that braces are inherently range operators, so this should actually do the trick for me:

    $max_lhs = $precision - $scale; $max_rhs = $scale; $float =~ m/ \A # beggining of string \d{0,$max_lhs} # 0 to $max_lhs digits \. # decimal point \d{0,$max_rhs} # 0 to $max_rhs digits \z # end of string /xms;

    I know the notion of what I'm calling 'precision' is confusing. I'm actually trying to check for the format of floats before inserting them into an Oracle number field and that's what Oracle calls them:

    precision - total number of digits
    scale - digits on the rhs

      If you say in your OP that 1 is valid then your regex will fail for that value because it expects to find a decimal point. Perhaps \.? making it optional would be better?

      Cheers,

      JohnGG

        Hmm... yes, but it's not really just the decimal that's optional, right? It's the decimal and the trailing digits. So something like this:

        $float =~ m/ \A # beggining of string \d{0,$max_lhs} # 0 to $max_lhs digits (?= # non capturing grouping \. # decimal point \d{1,$max_rhs} # 1 to $max_rhs digits )? # grouping is optional \z # end of string /xms;

        should do it, right? I haven't had a chance to test it, but that's the correct use of (?= )?, right?

      I'm confused. You say:

      precision - total number of digits
      scale - digits on the rhs

      but in you OP, you say that:

      with a precision of 4 and a scale of 3 (...) 12.3 is not valid

      Why not? Its total number of digits (3) is less than 'precision', and there is only one digiit on the rhs...

        This isn't really a Perl issue, but to clarify, in Oracle data types, precision defines the total number of digits and the scale defines the number of those digits that occur on the right side of the decimal. Thus, the number of possible digits on the left side of the decimal are $precision - $scale while the number of possible digits on the right hand side are $scale.
        When using MySQL your 12.3 would be translated to 12.300, because the scale is 3, which is not valid( mysql numeric types)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://604830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found