Numbers
Match a floating-point number
Match decimals like 3.14, -0.5, and scientific notation like 1.5e-10.
Pattern
Regular expression
-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?How it works
Optional sign, integer part, optional fractional part, optional exponent. Accepts both plain floats and scientific notation.
Matches
- ▸3.14
- ▸-0.5
- ▸1e10
- ▸1.5e-10
- ▸42
Non-matches
- ▸.5 (no integer part)
- ▸1e (missing exponent)
- ▸abc
Common gotchas
This pattern also matches plain integers like `42`. If you need to exclude them, require a decimal or exponent: `-?\\d+(?:\\.\\d+|[eE][+-]?\\d+)`.
Language snippets
JavaScript
const re = /-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g;Python
re.findall(r"-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?", text)Go
re := regexp.MustCompile(`-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?`)Java
Pattern.compile("-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?")Frequently asked questions
What is the regex for floating-point number?
The pattern -?\d+(?:\.\d+)?(?:[eE][+-]?\d+)? matches floating-point number. Optional sign, integer part, optional fractional part, optional exponent. Accepts both plain floats and scientific notation.
What does this floating-point number pattern match?
It matches strings like 3.14, -0.5, 1e10, but rejects .5 (no integer part), 1e (missing exponent).
How do I use this pattern in JavaScript, Python, Go, Java?
Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.
What are common pitfalls with a floating-point number regex?
This pattern also matches plain integers like `42`. If you need to exclude them, require a decimal or exponent: `-?\\d+(?:\\.\\d+|[eE][+-]?\\d+)`.