Regular expression for numbers
30 Dec 2023
It's easy to find a positive integer number with regular expressions:
[0-9]+
This regex means digits from 0 to 9, repeated one or more times. However, numbers starting with zero are treated as octal in many programming languages, so you may wish to avoid matching them:
[1-9][0-9]*
This regular expression matches any positive integer number starting with a non-zero digit. If you also need to match zero, you can include it as another branch:
[1-9][0-9]*|0
To also accomodate negative integer numbers, you can allow a minus sign before the digits:
-?[1-9][0-9]*|0
Sometimes it's necessary to allow a plus sign as well:
[-+]?[1-9][0-9]*|0
The previous regexes searched the input string for a number. If you need to match a number only discarding anything else, you can add the ^
anchor to match the beginning of the string and the $
anchor to match the end:
^(-?[1-9][0-9]*|0)$
Parentheses are necessary here; without them, the ^
anchor would apply only to the first branch. Another variation of the same regex avoids finding numbers that are part of words, such as 600px
or x64
:
\b(-?[1-9][0-9]*|0)\b
Things get more complicated if you need to match a fractional number:
\b-?(?:[1-9][0-9]*(?:\.[0-9]+)?|\.[0-9]+|0)\b
Let's break down this regular expression:
- The first branch
[1-9][0-9]*(?:\.[0-9]+)?
matches an integer number starting with a non-zero digit, then an optional fractional part. - The second branch
\.[0-9]+
matches fractional numbers starting with a dot, for example,.5
is another way to write0.5
. - The third branch matches zero. Note that both positive and negative zeros are possible in floating-point numbers.
For floating-point numbers with an exponent, such as 5.2777e+231
, please use:
\b-?(?:[1-9][0-9]*(?:\.[0-9]+)?|\.[0-9]+|0)(?:[eE][+-]?[0-9]+)?\b
Many programming languages support hexadecimal numbers starting with 0x
. Here is a regular expression to match them:
0x[0-9a-fA-F]+
Finally, here is a comprehensive regular expression to match floating-point, integer decimal, or hexadecimal numbers:
\b-?(?:[1-9][0-9]*(?:\.[0-9]+)?|\.[0-9]+|0(?:x[0-9a-fA-F]+)?)(?:[eE][+-]?[0-9]+)?\b
Replacing text in several files used to be a tedious and error-prone task. Aba Search and Replace solves the problem, allowing you to correct errors on your web pages, replace banners and copyright notices, change method names, and perform other text-processing tasks.
This is a blog about Aba Search and Replace, a tool for replacing text in multiple files.
- Automatically add width and height to img tags
- Using zero-width assertions in regular expressions
- Aba 2.7 released
- Regular Expressions 101
- 2023 in review
- Regular expression for numbers
- Aba 2.6 released
- Search from the Windows command prompt
- Empty character class in JavaScript regexes
- Privacy Policy Update - December 2022
- Aba 2.5 released
- Our response to the war in Ukraine
- Check VAT ID with regular expressions and VIES
- Which special characters must be escaped in regular expressions?
- Aba 2.4 released
- Privacy Policy Update - April 2021
- Review of Aba Search and Replace with video
- Aba 2.2 released
- Discount on Aba Search and Replace
- Using search and replace to rename a method
- Cleaning the output of a converter
- Aba 2.1 released
- How to replace HTML tags using regular expressions
- Video trailer for Aba
- Aba 2.0 released