Regex to Match Floating Point Numbers
Validate floating point numbers with this regex pattern. Matches decimal numbers with optional negative sign. Free online regex tool.
Regular Expression
/^-?\d+\.\d+$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| - | Matches the literal character '-' |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| \. | Matches a literal dot |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex matches floating point (decimal) numbers. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string, ensuring the entire string is validated.
-? — Matches an optional minus sign for negative numbers. The ? quantifier makes the hyphen character optional.
\d+ — Matches one or more digits before the decimal point. The \d shorthand matches any digit 0-9, and the + quantifier requires at least one digit. This means a decimal like '.5' without a leading digit will not match; you would need '0.5' instead.
. — Matches a literal decimal point. The backslash escapes the dot so it is not treated as the regex wildcard metacharacter. This dot is required, meaning plain integers will not match this pattern.
\d+ — Matches one or more digits after the decimal point. This requires at least one digit following the decimal point, so '3.' without trailing digits will not match.
$ — Anchors the match at the end of the string.
This pattern requires both a whole number part and a fractional part separated by a decimal point. It is strict in that it does not allow shorthand forms like '.5' or '3.' which some programming languages accept. If you need to match those forms as well, you would use a pattern with alternation like ^-?(?:\d+.\d*|.\d+|\d+)$. This pattern is ideal for validating user input in forms where you expect standard decimal notation.
Example Test Strings
| Input | Expected |
|---|---|
| 3.14 | Match |
| -0.5 | Match |
| 42 | No Match |
| .5 | No Match |
| 100.001 | Match |
Try It — Interactive Tester
12 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex