Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
It's quite often possible to construct a regex to discriminate a number range, but this is usually more of an academic exercise than a practical solution.

Turns out to not be too difficult if you cheat a little with some embedded code ;-) (of course hippo's suggestion is a lot more elegant Update: if that's the limit one wants to implement)

use Config; use Math::BigInt; my $regex = do { my $max = eval $Config{nv_overflows_integers_at} or die; my $len1 = length($max) - 1; my $range = substr $max, 0, 1; $range = $range eq "1" ? "1" : "1-$range"; qr{ \A (?: (?!0) [0-9]{1,$len1} | ( [$range] [0-9]{$len1} ) (?(?{ Math::BigInt->new($^N)->bgt($max) })(*F)) ) \z }x };

One difference to my first version is that this regex doesn't allow zero (0).

Update: Updated benchmark and faster version of the code here.

Tests

use warnings; use strict; use Config; use Math::BigInt; my $regex = do { my $max = eval $Config{nv_overflows_integers_at} or die; my $len1 = length($max) - 1; my $range = substr $max, 0, 1; $range = $range eq "1" ? "1" : "1-$range"; qr{ \A (?: (?!0) [0-9]{1,$len1} | ( [$range] [0-9]{$len1} ) (?(?{ Math::BigInt->new($^N)->bgt($max) })(*F)) ) \z }x }; use Test::More; diag explain $regex; unlike 0, $regex; like 1, $regex; like 3, $regex; unlike "", $regex; unlike "x", $regex; unlike "123y", $regex; unlike -1, $regex; unlike "-9999999999999999999999999999999", $regex; my $x = eval($Config{nv_overflows_integers_at})-1; like "$x", $regex, "max-1 ($x)"; $x++; like "$x", $regex, "max works ($x)"; $x++; unlike "$x", $regex, "max+1 fails ($x)"; $x+=1_000; unlike "$x", $regex; unlike "999999999999999999999999999999999999", $regex; done_testing;

Benchmark (vs. my first version):

#!/usr/bin/env perl use warnings; use strict; use Benchmark qw/cmpthese/; use feature 'state'; use Carp; use Math::BigInt; use Config; use Regexp::Common qw/number/; sub validate_int { my $str = shift; state $max = Math::BigInt->new( eval $Config{nv_overflows_integers_at} ); croak "not an integer" unless defined $str && $str=~/\A$RE{num}{int}\z/; my $num = Math::BigInt->new($str); croak "integer to small" if $num < 0; croak "integer too big" if $num > $max; return $num->numify; } sub validate_int2 { my $str = shift; state $regex = do { my $max = eval $Config{nv_overflows_integers_at} or die; my $len1 = length($max) - 1; my $range = substr $max, 0, 1; $range = $range eq "1" ? "1" : "1-$range"; qr{ \A (?: (?!0) [0-9]{1,$len1} | ( [$range] [0-9]{$len1} ) (?(?{ Math::BigInt->new($^N)->bgt($max) })(*F)) ) \z }x }; croak "bad integer" unless $str =~ $regex; return 0+$str; } my $max = eval($Config{nv_overflows_integers_at}); cmpthese(-1, { first => sub { validate_int(1)==1 or die; validate_int(999)==999 or die; validate_int($max-1)==$max-1 or die; validate_int($max)==$max or die; defined eval { validate_int($max+1) } and die; }, second => sub { validate_int2(1)==1 or die; validate_int2(999)==999 or die; validate_int2($max-1)==$max-1 or die; validate_int2($max)==$max or die; defined eval { validate_int2($max+1) } and die; }, }); __END__ Rate first second first 4403/s -- -70% second 14902/s 238% --

In reply to Re^4: Reliably parsing an integer (updated) by haukex
in thread Reliably parsing an integer by rdiez

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2020-09-19 19:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If at first I donít succeed, I Ö










      Results (115 votes). Check out past polls.

      Notices?