6.5. Decimal Numbers

Problem

You want to find various kinds of integer decimal numbers in a larger body of text, or check whether a string variable holds an integer decimal number. The number must not have a leading zero, as only octal numbers can have leading zeros. But the number zero itself is a valid decimal number.

Solution

Find any positive integer decimal number without a leading zero in a larger body of text:

\b(0|[1-9][0-9]*)\b
Regex options: None
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Check whether a text string holds just a positive integer decimal number without a leading zero:

\A(0|[1-9][0-9]*)\Z
Regex options: None
Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby
^(0|[1-9][0-9]*)$
Regex options: None
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python

Discussion

Recipe 6.1 shows a lot of solutions for matching integer decimal numbers, along with a detailed explanation. But the solutions in that recipe do not take into account that in many programming languages, numbers with a leading zero are octal numbers rather than decimal numbers. They simply use [0-9]+ to match any sequence of decimal digits.

The solutions in this recipe exclude numbers with a leading zero, while still matching the number zero itself. Instead of matching any sequence of decimal digits with [0-9]+, these regular expressions use 0|[1-9][0-9]* to match either the digit zero, or a decimal number with at least one digit that does not begin with a zero. Since the alternation ...

Get Regular Expressions Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.