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.
Find any positive integer decimal number without a leading zero in a larger body of text:
(0|[1-9][0-9]*)
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]*)
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 |
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 operator has the lowest
precedence of all regular expression operators, we use a group to make
sure the anchors and word boundaries stay outside of the alternation.
Recipe 6.4 has solutions for matching octal numbers.
Techniques used in the regular expressions in this recipe are discussed in Chapter 2. Recipe 2.3 explains character classes. Recipe 2.5 explains anchors. Recipe 2.6 explains word boundaries. Recipe 2.8 explains alternation. Recipe 2.9 explains grouping. Recipe 2.12 explains repetition.