/usr/share/perl5/Regexp/Common/number.pm is in libregexp-common-perl 2016060801-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | package Regexp::Common::number;
use 5.10.0;
use strict;
use warnings;
no warnings 'syntax';
use Config;
use Regexp::Common qw /pattern clean no_defaults/;
our $VERSION = '2016060801';
sub _croak {
require Carp;
goto &Carp::croak;
}
my $digits = join ("", 0 .. 9, "A" .. "Z");
sub int_creator {
my $flags = $_ [1];
my ($sep, $group, $base, $places, $sign) =
@{$flags} {qw /-sep -group -base -places -sign/};
# Deal with the bases.
_croak "Base must be between 1 and 36" unless $base >= 1 &&
$base <= 36;
my $chars = substr $digits, 0, $base;
$sep = ',' if exists $flags -> {-sep} && !defined $flags -> {-sep};
my $max = $group;
$max = $2 if $group =~ /^\s*(\d+)\s*,\s*(\d+)\s*$/;
my $quant = $places ? "{$places}" : "+";
return $sep ? qq {(?k:(?k:$sign)(?k:[$chars]{1,$max}} .
qq {(?:$sep} . qq {[$chars]{$group})*))}
: qq {(?k:(?k:$sign)(?k:[$chars]$quant))}
}
sub real_creator {
my ($base, $places, $radix, $sep, $group, $expon, $sign) =
@{$_[1]}{-base, -places, -radix, -sep, -group, -expon, -sign};
_croak "Base must be between 1 and 36"
unless $base >= 1 && $base <= 36;
$sep = ',' if exists $_[1]->{-sep}
&& !defined $_[1]->{-sep};
if ($base > 14 && $expon =~ /^[Ee]$/) {$expon = 'G'}
foreach ($radix, $sep, $expon) {$_ = "[$_]" if 1 == length}
my $chars = substr $digits, 0, $base;
return $sep
? qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
qq {(?k:[$chars]{1,$group}(?:(?:$sep)[$chars]{$group})*)} .
qq {(?:(?k:$radix)(?k:[$chars]{$places}))?)} .
qq {(?:(?k:$expon)(?k:(?k:$sign)(?k:[$chars]+))|))}
: qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
qq {(?k:[$chars]*)(?:(?k:$radix)(?k:[$chars]{$places}))?)} .
qq {(?:(?k:$expon)(?k:(?k:$sign)(?k:[$chars]+))|))};
}
sub decimal_creator {
my ($base, $places, $radix, $sep, $group, $sign) =
@{$_[1]}{-base, -places, -radix, -sep, -group, -sign};
_croak "Base must be between 1 and 36"
unless $base >= 1 && $base <= 36;
$sep = ',' if exists $_[1]->{-sep}
&& !defined $_[1]->{-sep};
foreach ($radix, $sep) {$_ = "[$_]" if 1 == length}
my $chars = substr $digits, 0, $base;
return $sep
? qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
qq {(?k:[$chars]{1,$group}(?:(?:$sep)[$chars]{$group})*)} .
qq {(?:(?k:$radix)(?k:[$chars]{$places}))?))}
: qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
qq {(?k:[$chars]*)(?:(?k:$radix)(?k:[$chars]{$places}))?))}
}
pattern name => [qw (num int -sep= -base=10 -group=3 -sign=[-+]?)],
create => \&int_creator,
;
pattern name => [qw (num real -base=10), '-places=0,',
qw (-radix=[.] -sep= -group=3 -expon=E -sign=[-+]?)],
create => \&real_creator,
;
pattern name => [qw (num decimal -base=10), '-places=0,',
qw (-radix=[.] -sep= -group=3 -sign=[-+]?)],
create => \&decimal_creator,
;
sub real_synonym {
my ($name, $base) = @_;
pattern name => ['num', $name, '-places=0,', '-radix=[.]',
'-sep=', '-group=3', '-expon=E', '-sign=[-+]?'],
create => sub {my %flags = (%{$_[1]}, -base => $base);
real_creator (undef, \%flags);
}
;
}
real_synonym (hex => 16);
real_synonym (dec => 10);
real_synonym (oct => 8);
real_synonym (bin => 2);
# 2147483647 == 2^31 - 1
# 9223372036854775807 == 2^63 - 1
pattern name => [qw (num square)],
create => sub {
use re 'eval';
my $sixty_four_bits = $Config {use64bitint};
#
# CPAN testers claim it fails on 5.8.8 and darwin 9.0.
#
my $num = $sixty_four_bits
? '0*(?:(?:9(?:[0-1][0-9]{17}' .
'|2(?:[0-1][0-9]{16}' .
'|2(?:[0-2][0-9]{15}' .
'|3(?:[0-2][0-9]{14}' .
'|3(?:[0-6][0-9]{13}' .
'|7(?:[0-1][0-9]{12}' .
'|20(?:[0-2][0-9]{10}' .
'|3(?:[0-5][0-9]{9}' .
'|6(?:[0-7][0-9]{8}' .
'|8(?:[0-4][0-9]{7}' .
'|5(?:[0-3][0-9]{6}' .
'|4(?:[0-6][0-9]{5}' .
'|7(?:[0-6][0-9]{4}' .
'|7(?:[0-4][0-9]{3}' .
'|5(?:[0-7][0-9]{2}' .
'|80(?:[0-6])))))))))))))))))|[1-8]?[0-9]{0,18})'
: '0*(?:2(?:[0-0][0-9]{8}' .
'|1(?:[0-3][0-9]{7}' .
'|4(?:[0-6][0-9]{6}' .
'|7(?:[0-3][0-9]{5}' .
'|4(?:[0-7][0-9]{4}' .
'|8(?:[0-2][0-9]{3}' .
'|3(?:[0-5][0-9]{2}' .
'|6(?:[0-3][0-9]{1}' .
'|4[0-7])))))))))|1?[0-9]{1,9}';
qr {($num)(?(?{length $^N && sqrt ($^N) == int sqrt ($^N)})|(?!))}
},
;
pattern name => [qw (num roman)],
create => '(?xi)(?=[MDCLXVI])
(?k:M{0,4}
(?:C[DM]|D?C{0,4})?
(?:X[LC]|L?X{0,4})?
(?:I[VX]|V?I{0,4})?)'
;
1;
__END__
=pod
=head1 NAME
Regexp::Common::number -- provide regexes for numbers
=head1 SYNOPSIS
use Regexp::Common qw /number/;
while (<>) {
/^$RE{num}{int}$/ and print "Integer\n";
/^$RE{num}{real}$/ and print "Real\n";
/^$RE{num}{real}{-base => 16}$/ and print "Hexadecimal real\n";
}
=head1 DESCRIPTION
Please consult the manual of L<Regexp::Common> for a general description
of the works of this interface.
Do not use this module directly, but load it via I<Regexp::Common>.
=head2 C<$RE{num}{int}{-base}{-sep}{-group}{-places}{-sign}>
Returns a pattern that matches an integer.
If C<< -base => I<B> >> is specified, the integer is in base I<B>, with
C<< 2 <= I<B> <= 36 >>. For bases larger than 10, upper case letters
are used. The default base is 10.
If C<< -sep => I<P> >> is specified, the pattern I<P> is required as a
grouping marker within the number. If this option is not given, no
grouping marker is used.
If C<< -group => I<N> >> is specified, digits between grouping markers
must be grouped in sequences of exactly I<N> digits. The default value
of I<N> is 3. If C<< -group => I<N,M> >> is specified, digits between
grouping markers must be grouped in sequences of at least I<N> digits,
and at most I<M> digits. This option is ignored unless the C<< -sep >>
option is used.
If C<< -places => I<N> >> is specified, the integer recognized must be
exactly I<N> digits wide. If C<< -places => I<N,M> >> is specified, the
integer must be at least I<N> wide, and at most I<M> characters. There
is no default, which means that integers are unlimited in size. This
option is ignored if the C<< -sep >> option is used.
If C<< -sign => I<P> >> is used, it's a pattern the leading sign has to
match. This defaults to C<< [-+]? >>, which means the number is optionally
preceded by a minus or a plus. If you want to match unsigned integers,
use C<< $RE{num}{int}{-sign => ''} >>.
For example:
$RE{num}{int} # match 1234567
$RE{num}{int}{-sep=>','} # match 1,234,567
$RE{num}{int}{-sep=>',?'} # match 1234567 or 1,234,567
$RE{num}{int}{-sep=>'.'}{-group=>4} # match 1.2345.6789
Under C<-keep> (see L<Regexp::Common>):
=over 4
=item $1
captures the entire number
=item $2
captures the optional sign of the number
=item $3
captures the complete set of digits
=back
=head2 C<$RE{num}{real}{-base}{-radix}{-places}{-sep}{-group}{-expon}>
Returns a pattern that matches a floating-point number.
If C<-base=I<N>> is specified, the number is assumed to be in that base
(with A..Z representing the digits for 11..36). By default, the base is 10.
If C<-radix=I<P>> is specified, the pattern I<P> is used as the radix point for
the number (i.e. the "decimal point" in base 10). The default is C<qr/[.]/>.
If C<-places=I<N>> is specified, the number is assumed to have exactly
I<N> places after the radix point.
If C<-places=I<M,N>> is specified, the number is assumed to have between
I<M> and I<N> places after the radix point.
By default, the number of places is unrestricted.
If C<-sep=I<P>> specified, the pattern I<P> is required as a grouping marker
within the pre-radix section of the number. By default, no separator is
allowed.
If C<-group=I<N>> is specified, digits between grouping separators
must be grouped in sequences of exactly I<N> characters. The default value of
I<N> is 3.
If C<-expon=I<P>> is specified, the pattern I<P> is used as the exponential
marker. The default value of I<P> is C<qr/[Ee]/>.
If C<-sign=I<P>> is specified, the pattern I<P> is used to match the
leading sign (and the sign of the exponent). This defaults to C<< [-+]? >>,
means means that an optional plus or minus sign can be used.
For example:
$RE{num}{real} # matches 123.456 or -0.1234567
$RE{num}{real}{-places=>2} # matches 123.45 or -0.12
$RE{num}{real}{-places=>'0,3'} # matches 123.456 or 0 or 9.8
$RE{num}{real}{-sep=>'[,.]?'} # matches 123,456 or 123.456
$RE{num}{real}{-base=>3'} # matches 121.102
Under C<-keep>:
=over 4
=item $1
captures the entire match
=item $2
captures the optional sign of the number
=item $3
captures the complete mantissa
=item $4
captures the whole number portion of the mantissa
=item $5
captures the radix point
=item $6
captures the fractional portion of the mantissa
=item $7
captures the optional exponent marker
=item $8
captures the entire exponent value
=item $9
captures the optional sign of the exponent
=item $10
captures the digits of the exponent
=back
=head2 C<$RE{num}{dec}{-radix}{-places}{-sep}{-group}{-expon}>
A synonym for C<< $RE{num}{real}{-base=>10}{...} >>
=head2 C<$RE{num}{oct}{-radix}{-places}{-sep}{-group}{-expon}>
A synonym for C<< $RE{num}{real}{-base=>8}{...} >>
=head2 C<$RE{num}{bin}{-radix}{-places}{-sep}{-group}{-expon}>
A synonym for C<< $RE{num}{real}{-base=>2}{...} >>
=head2 C<$RE{num}{hex}{-radix}{-places}{-sep}{-group}{-expon}>
A synonym for C<< $RE{num}{real}{-base=>16}{...} >>
=head2 C<$RE{num}{decimal}{-base}{-radix}{-places}{-sep}{-group}>
The same as C<$RE{num}{real}>, except that an exponent isn't allowed.
Hence, this returns a pattern matching I<decimal> numbers.
If C<-base=I<N>> is specified, the number is assumed to be in that base
(with A..Z representing the digits for 11..36). By default, the base is 10.
If C<-radix=I<P>> is specified, the pattern I<P> is used as the radix point for
the number (i.e. the "decimal point" in base 10). The default is C<qr/[.]/>.
If C<-places=I<N>> is specified, the number is assumed to have exactly
I<N> places after the radix point.
If C<-places=I<M,N>> is specified, the number is assumed to have between
I<M> and I<N> places after the radix point.
By default, the number of places is unrestricted.
If C<-sep=I<P>> specified, the pattern I<P> is required as a grouping marker
within the pre-radix section of the number. By default, no separator is
allowed.
If C<-group=I<N>> is specified, digits between grouping separators
must be grouped in sequences of exactly I<N> characters. The default value of
I<N> is 3.
For example:
$RE{num}{decimal} # matches 123.456 or -0.1234567
$RE{num}{decimal}{-places=>2} # matches 123.45 or -0.12
$RE{num}{decimal}{-places=>'0,3'} # matches 123.456 or 0 or 9.8
$RE{num}{decimal}{-sep=>'[,.]?'} # matches 123,456 or 123.456
$RE{num}{decimal}{-base=>3'} # matches 121.102
Under C<-keep>:
=over 4
=item $1
captures the entire match
=item $2
captures the optional sign of the number
=item $3
captures the complete mantissa
=item $4
captures the whole number portion of the mantissa
=item $5
captures the radix point
=item $6
captures the fractional portion of the mantissa
=back
=head2 C<$RE{num}{square}>
Returns a pattern that matches a (decimal) square. Because Perl's
arithmetic is lossy when using integers over about 53 bits, this pattern
only recognizes numbers less than 9000000000000000, if one uses a
Perl that is configured to use 64 bit integers. Otherwise, the limit
is 2147483647. These restrictions were introduced in versions 2.116
and 2.117 of Regexp::Common. Regardless whether C<-keep> was set,
the matched number will be returned in C<$1>.
=head2 C<$RE{num}{roman}>
Returns a pattern that matches an integer written in Roman numbers.
Case doesn't matter. There is no unique way of writing Roman numerals,
but we will not match anything. We require the Roman numerals to
list the symbols in order (largest first). The symbols for thousand
(C<< M >>), hundred (C<< C >>), ten (C<< X >>), and one (C<< I >>)
can not be repeated more than four times. The symbols for five hundred
(C<< D >>), fifty (C<< L >>), and five (C<< V >>) may not appear more
than once. A sequence of four repeated characters may also be written
as a subtraction: by using the repeated character just once, and have
it followed by the symbol which is 5 or 10 as large. So, four can be
written as C<< IIII >>, or as C<< IV >>, and nine may be written as
C<< VIIII >> or C<< IX >>. This corresponds to most modern uses of
Roman numerals.
The largest number which will be matched is 4999, or
C<< MMMMDCCCCLXXXXVIIII >>, or C<< MMMMCMXCIX >>.
Under C<-keep>, the number will be captured in $1.
=head1 SEE ALSO
L<Regexp::Common> for a general description of how to use this interface.
=head1 AUTHOR
Damian Conway (damian@conway.org)
=head1 MAINTENANCE
This package is maintained by Abigail S<(I<regexp-common@abigail.be>)>.
=head1 BUGS AND IRRITATIONS
Bound to be plenty.
For a start, there are many common regexes missing.
Send them in to I<regexp-common@abigail.be>.
=head1 LICENSE and COPYRIGHT
This software is Copyright (c) 2001 - 2016, Damian Conway and Abigail.
This module is free software, and maybe used under any of the following
licenses:
1) The Perl Artistic License. See the file COPYRIGHT.AL.
2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2.
3) The BSD License. See the file COPYRIGHT.BSD.
4) The MIT License. See the file COPYRIGHT.MIT.
=cut
|