in reply to Re: Regex for max length float
in thread Regex for max length float

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

Replies are listed 'Best First'.
Re^3: Regex for max length float
by johngg (Canon) on Mar 14, 2007 at 15:37 UTC
    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?

        (?:...) is non-capturing grouping.
        (?=...) is zero-width positive lookahead, which isn't appropriate here.

Re^3: Regex for max length float
by Not_a_Number (Prior) on Mar 14, 2007 at 17:16 UTC

    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)