This might get the creative juices flowing. It does output as you'd expect, but there is a limitation in this sample. Namely, the '$predec' is empty and the '$postdec' is full when there is no decimal point in the number. Anyhow... just wanted to do something fun before lunch. If I was a REGEX guru (I'm not even close to that planet) I would think that a look-ahead/behind would be useful in this case.

Be kind, this is my first code post. :)

KPH
use strict; use warnings; my $precision = 4; my $scale = 3; my $regex = qr{\A(\d*?)\.?(\d*?)\z}; foreach my $float ("1", ".45", "3.452", "3.4526", "12.3", ".34567", "N +aN") { if (my ($predec,$postdec) = ($float =~ m/$regex/)) { if (length "$postdec" > $scale) { print "Invalid scale in $float ($predec\t$post +dec)\n"; } elsif (length "$predec" > $precision - $scale) { print "Invalid precision in $float ($predec\t +$postdec)\n"; } else { print "Valid number: $float ($predec\t$postdec +)\n"; } } else { print "Invalid floating point number: $float\n"; } }

In reply to Re: Regex for max length float by whereiskurt
in thread Regex for max length float by agianni

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.