in reply to Regex for max length float
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. :)
KPHuse 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"; } }
|
|---|