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

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found