Regex to Match a Decimal Number
Copy the pattern, paste your text below, and see every decimal it matches. Below the tester: how it handles signs, leading dots, and integers.
Regex pattern
/[-+]?(?:\d+\.?\d*|\.\d+)/g
This matches a decimal number with an optional sign and optional fractional part, including 42, 3.14, -.5 and +7. It does not match thousands separators or scientific notation.
What this pattern matches
This matches decimal numbers with an optional sign, whether written as 3.14, 42, or .75.
How it works, part by part
[-+]? — an optional leading sign.
\d+\.?\d* — digits with an optional decimal point and more digits (covers 42 and 3.14).
|\.\d+ — or a leading-dot form like .75.
Strict validation
To validate a whole field as one number, anchor it with ^…$. As written it will find each number inside 1.2.3 separately rather than rejecting it.
Use it in your code
import re
text = "Valid: 3.14, -0.5, +42, .75, 100. Not matched cleanly: 1.2.3 (two numbers)."
pattern = r'''[-+]?(?:\d+\.?\d*|\.\d+)'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Valid: 3.14, -0.5, +42, .75, 100. Not matched cleanly: 1.2.3 (two numbers).";
const re = new RegExp(String.raw`[-+]?(?:\d+\.?\d*|\.\d+)`, "g");
console.log(text.match(re));
FAQ
Does it match leading-dot numbers like .5?
Yes. The second alternative \.\d+ handles .5 and .75.
Does it accept a plus sign?
Yes. The optional [-+] leading sign matches both +42 and -0.5.
How do I reject 1.2.3?
Anchor the pattern with ^ and $ so the whole input must be a single valid number.