Regex to Match a Negative Number
Copy the pattern, paste your text, and watch every negative value light up. Below: how to include or exclude decimals.
What this pattern matches
It matches a negative number — a minus sign followed by an integer, with an optional decimal part.
Part by part
- — a literal minus sign (this is what makes it negative).
\d+ — one or more digits before the decimal point.
(?:\.\d+)? — an optional fractional part.
Variations
Integers only: drop the decimal group, -\d+. Accept the Unicode minus − as well: [-−]\d+(?:\.\d+)?. Anchor with ^…$ to validate a single field.
Use it in your code
import re
text = "Temperatures: -5, 3.2, -3.14, 0, -0.5, 12, -100"
pattern = r'''-\d+(?:\.\d+)?'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Temperatures: -5, 3.2, -3.14, 0, -0.5, 12, -100";
const re = new RegExp(String.raw`-\d+(?:\.\d+)?`, "g");
console.log(text.match(re));
FAQ
Does it match a bare minus like '- 5'?
No. There must be no space between the sign and the digits. Add \s* between them if your input allows it.
Why doesn't 0 match?
Zero has no sign, so it is neither positive nor negative here. -0 would match the pattern.
How do I match negative numbers with thousands separators?
Add the group: -\d{1,3}(?:,\d{3})*(?:\.\d+)? for values like -1,234.50.