Updated: 2003-03-23

Index Return to Main Contents

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"; }

Do not use this module directly, but load it via *Regexp::Common*.

If `"-base => `B`"` is specified, the integer is in base *B*, with
`"2 <= `B` <= 36"`. For bases larger than 10, upper case letters
are used. The default base is 10.

If `"-sep => `P`"` is specified, the pattern *P* is required as a
grouping marker within the number. If this option is not given, no
grouping marker is used.

If `"-group => `N`"` is specified, digits between grouping markers
must be grouped in sequences of exactly *N* digits. The default value
of *N* is 3. If `"-group => `N,M`"` is specified, digits between
grouping markers must be grouped in sequences of at least *N* digits,
and at most *M* digits. This option is ignored unless the `"-sep"`
option is used.

If `"-places => `N`"` is specified, the integer recognized must be
exactly *N* digits wide. If `"-places => `N,M`"` is specified, the
integer must be at least *N* wide, and at most *M* characters. There
is no default, which means that integers are unlimited in size. This
option is ignored if the `"-sep"` option is used.

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 `"-keep"` (see Regexp::Common):

- $1
- captures the entire number
- $2
- captures the optional sign of the number
- $3
- captures the complete set of digits

If `"-base=`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 `"-radix=`P`"` is specified, the pattern *P* is used as the radix point for
the number (i.e. the ``decimal point'' in base 10). The default is `"qr/[.]/"`.

If `"-places=`N`"` is specified, the number is assumed to have exactly
*N* places after the radix point.
If `"-places=`M,N`"` is specified, the number is assumed to have between
*M* and *N* places after the radix point.
By default, the number of places is unrestricted.

If `"-sep=`P`"` specified, the pattern *P* is required as a grouping marker
within the pre-radix section of the number. By default, no separator is
allowed.

If `"-group=`N`"` is specified, digits between grouping separators
must be grouped in sequences of exactly *N* characters. The default value of
*N* is 3.

If `"-expon=`P`"` is specified, the pattern *P* is used as the exponential
marker. The default value of *P* is `"qr/[Ee]/"`.

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 `"-keep"`:

- $1
- captures the entire match
- $2
- captures the optional sign of the number
- $3
- captures the complete mantissa
- $4
- captures the whole number portion of the mantissa
- $5
- captures the radix point
- $6
- captures the fractional portion of the mantissa
- $7
- captures the optional exponent marker
- $8
- captures the entire exponent value
- $9
- captures the optional sign of the exponent
- $10
- captures the digits of the exponent

If `"-base=`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 `"-radix=`P`"` is specified, the pattern *P* is used as the radix point for
the number (i.e. the ``decimal point'' in base 10). The default is `"qr/[.]/"`.

If `"-places=`N`"` is specified, the number is assumed to have exactly
*N* places after the radix point.
If `"-places=`M,N`"` is specified, the number is assumed to have between
*M* and *N* places after the radix point.
By default, the number of places is unrestricted.

If `"-sep=`P`"` specified, the pattern *P* is required as a grouping marker
within the pre-radix section of the number. By default, no separator is
allowed.

If `"-group=`N`"` is specified, digits between grouping separators
must be grouped in sequences of exactly *N* characters. The default value of
*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 `"-keep"`:

- $1
- captures the entire match
- $2
- captures the optional sign of the number
- $3
- captures the complete mantissa
- $4
- captures the whole number portion of the mantissa
- $5
- captures the radix point
- $6
- captures the fractional portion of the mantissa

This pattern is available for version 5.008 and up.

Under `"-keep"`, the number will be captured in `$1`.

$Log: number.pm,v $ Revision 2.108 2005/03/16 00:25:58 abigail Added -base, -places for {num} {int}. Changed -group

Revision 2.107 2004/12/28 23:45:51 abigail Perl 5.6.2 parses qq lib/Regexp/Common/number.pm{sep}[0-9]! incorrectly

Revision 2.106 2004/12/28 23:27:58 abigail Replaced C<\d> with [0-9] (Unicode reasons)

Revision 2.105 2004/07/01 10:11:27 abigail Fixed problems with 32bit integer Perls

Revision 2.104 2004/06/30 09:14:54 abigail Restricted recognition of square numbers to numbers less than 9000000000000000 to avoid round-off errors.

Revision 2.103 2003/03/12 22:24:25 abigail Decimal numbers

Revision 2.102 2003/02/10 21:34:24 abigail Added VERSION

Revision 2.101 2003/02/01 22:55:31 abigail Changed Copyright years

Revision 2.100 2003/01/21 23:19:40 abigail The whole world understands RCS/CVS version numbers, that 1.9 is an older version than 1.10. Except CPAN. Curse the idiot(s) who think that version numbers are floats (in which universe do floats have more than one decimal dot?). Everything is bumped to version 2.100 because CPAN couldn't deal with the fact one file had version 1.10.

Revision 1.6 2002/12/27 23:33:15 abigail Roman numbers.

Revision 1.5 2002/08/23 13:09:13 abigail Cosmetic POD changes.

Revision 1.4 2002/08/23 12:51:26 abigail + Several occurances of 'numbers' changed to 'number'. + Fixed bugs in documentation. + Made example use anchors to make it more clear. (All due to Christopher Baker)

Revision 1.3 2002/08/05 12:16:59 abigail Fixed 'Regex::' and 'Rexexp::' typos to 'Regexp::' (Found by Mike Castle).

Revision 1.2 2002/07/30 16:37:59 abigail Removed outcommented code.

Revision 1.1 2002/07/28 21:41:07 abigail Split off from Regexp::Common.

For a start, there are many common regexes missing.
Send them in to *regexp-common@abigail.nl*.

Copyright (c) 2001 - 2003, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the terms of the Perl Artistic License (see http://www.perl.com/perl/misc/Artistic.html)

- NAME
- SYNOPSIS
- DESCRIPTION
- $RE{num}{int}{-base}{-sep}{-group}{-places}
- $RE{num}{real}{-base}{-radix}{-places}{-sep}{-group}{-expon}
- $RE{num}{dec}{-radix}{-places}{-sep}{-group}{-expon}
- $RE{num}{oct}{-radix}{-places}{-sep}{-group}{-expon}
- $RE{num}{bin}{-radix}{-places}{-sep}{-group}{-expon}
- $RE{num}{hex}{-radix}{-places}{-sep}{-group}{-expon}
- $RE{num}{decimal}{-base}{-radix}{-places}{-sep}{-group}
- $RE{num}{square}
- $RE{num}{roman}
- HISTORY
- SEE ALSO
- AUTHOR
- MAINTAINANCE
- BUGS AND IRRITATIONS
- COPYRIGHT