Regular Expression Library :: Numbers

This page contains a collection of regular expressions for matching numbers in various formats. To match a string that matches one of these expressions as a whole rather than in part, put a ^ before the expression and a $ after it. For example, \d+ becomes ^\d+$.

Integer

Description: Matches any whole number, either positive or negative, allowing -0.
Perl Compatible Form: -?\d+
POSIX Form: -?[0-9]+

Integer With -0 Disallowed

Description: Matches any whole number, either positive or negative, not allowing -0.
Perl Compatible Form: -?0*[1-9]\d*|0+
POSIX Form: -?0*[1-9][0-9]*|0+

Integer With No Leading Zeroes

Description: Matches any whole number, either positive or negative, except when it has leading zeroes (that is, 4 and -2 will match, but 04 and -02 will not). -0 will match.
Perl Compatible Form: -?([1-9]\d*|0)
POSIX Form: -?([1-9][0-9]*|0)

Integer With No Leading Zeroes And -0 Disallowed

Description: Matches any whole number, either positive or negative, except when it has leading zeroes (that is, 4 and -2 will match, but 04 and -02 will not). -0 will not match.
Perl Compatible Form: -?[1-9]\d*|0
POSIX Form: -?([1-9][0-9]*|0)

Positive Integers

Description: A positive integer, excluding zero and with no leading zeroes allowed. Add |0 on the end to allow zero.
Perl Compatible Form: [1-9]\d*
POSIX Form: [1-9][0-9]*

Positive Integer Explicitly Rejecting Negatives

Description: A positive integer, excluding zero and with no leading zeroes allowed. Add |0 on the end to allow zero. This expression is useful when checking if a string contains a positive number and ignoring negatives. Note that the grouping (brackets) will count as a capture.
Perl Compatible Form: (^|[^-])[1-9]\d*
POSIX Form: (^|[^-])[1-9][0-9]*

Decimals

Description: Any decimal number. Remove the -? at the start to only allow positive decimals and change -? to - to only allow negative decimals.
Perl Compatible Form: -?\d+(\.\d+)?
POSIX Form: -?[0-9]+(\.[0-9]+)?

Number With Exponent

Description: Any decimal number with an optional decimal exponent. For example, 2.53e10 means the number 2.53 raised to the 10th power of ten (provided we are working in base 10)..
Perl Compatible Form: -?\d+(\.\d+)?(e\d+(\.\d+)?)?
POSIX Form: -?[0-9]+(\.[0-9]+)(e[0-9]+(\.[0-9]+)?)?

Binary (Base 2)

To match binary numbers, use the above expressions but re-write every instance of \d or [0-9] to be [01]. Any character class of the form [1-9] should be re-written written as 1.

Hex (Base 16)

To match hex numbers, use the above expressions but re-write every instance of \d or [0-9] to be [0-9A-F]. Any character class of the form [1-9] should be re-written as [1-9A-F].

Octal (Base 8)

To match octal numbers, use the above expressions but re-write every instance of \d or [0-9] to be [0-7]. Any character class of the form [1-9] should be re-written as [1-7].

 

Other Views

corner

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.