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].

  User Comments


Anonymous
(Not rated)
(Report as abusive)
new String("123")????
new String(<literal>) is a newbie mistake... just use the literal directly.
Anonymous

(Report as abusive)
the comment above is incorrect
It is true that declaring variables unnecessarily is a frequent newbie mistake. However, in this case, it is necessary to have a String object to operate on.

For example, if you don't put the string in var text here, you can't use the replace method on it:

var text = "abababab";
var altered = text.replace(/b/, 'a');

Would you write that as
var altered = "abababab".replace(/b/, 'a');

I think not.

However, I do think that there is a typo in the telephone number example. The string is placed into a variable named "phone", so the second line should probably be:
var lastfour = phone.match(/\d{4}$/);

instead of
var lastfour = text.match(/\d{4}$/);

which (probably inadvertently) references a variable named "text" which is used in adjacent examples.
  View all   Rate and comment this article




 
Printer friendly version of the RegexLibraryNumbers page


Sponsored links

PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
Software Localization Tool Sisulizer
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now
Attend WINDOWS EMBEDDED ACCELERATION WORKSHOPS
Are you ready to learn how you can bring your next-generation embedded device to market faster?
SSH and SFTP support for .NET
Add complete SSH and SFTP support to your .NET framework application

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd 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 Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.