Regex tester · Guide
Regex Match Numbers: Integers, Decimals and Negatives
Matching a number looks like the simplest regex task and quietly is not. `\d+` matches Arabic-Indic digits in several engines, accepts leading zeros, and says nothing about decimals or signs.
The building blocks
\d+ # one or more digits
-?\d+ # optional minus
[+-]?\d+ # optional sign
\d+\.\d+ # decimal, digits required both sides
-?\d+(?:\.\d+)? # optional decimal part
-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)? # scientific notationEscape the dot. An unescaped . in \d+.\d+ matches any character, so 12x34 passes as a decimal.
Anchoring and boundaries
Unanchored, \d+ finds a number inside anything — abc123def matches. For validation, anchor with ^ and $; for extraction from text, use word boundaries \b\d+\b so abc123 is not treated as the number 123.
Note \b treats - as a boundary, so \b-?\d+\b will not capture the minus in -5. Use (?<![\w.])-?\d+ when the sign matters.
The Unicode surprise
In Python, Java and .NET, \d matches every Unicode decimal digit — including Arabic-Indic ٤٢ and Devanagari ४२. int() in Python will happily parse those, which may or may not be what you want.
Restrict to ASCII explicitly when the value feeds something that expects it: [0-9] instead of \d, or Python re.ASCII. In JavaScript, \d is always ASCII-only, which is one fewer thing to think about.
When not to use regex
Formatted numbers with thousands separators, currency symbols or locale-specific decimal commas get ugly fast — ^-?\d{1,3}(?:,\d{3})*(?:\.\d+)?$ handles one convention and fails on European formatting.
Regex is good at finding candidate numbers in text. Validating a range, checking precision or parsing a locale belongs in code: match loosely, then parse and check properly.
Frequently asked questions
Why does \d+.\d+ match 12x34?
The dot is unescaped and matches any character. Write \d+\.\d+.
Does \d match only 0-9?
In JavaScript yes. In Python, Java and .NET it matches Unicode digits too unless you restrict it.
How do I match a negative decimal?
Use -?\d+(?:\.\d+)? and anchor it if you are validating rather than searching.
Ready to try it?
Open the free browser-based Regex tester and apply what you just read — no sign-up, runs locally.
Open the Regex tester tool