Regex to Match Negative Floating-Point Numbers
Validate negative floating-point numbers with a mandatory minus sign and decimal point. Matches numbers like -3.14, -0.5, and -.75 precisely. Free regex tester.
Regular Expression
/^-(?:0|[1-9]\d*)?\.\d+$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| - | Matches the literal character '-' |
| (?: | Start of non-capturing group |
| 0 | Matches the literal character '0' |
| | | Alternation — matches the expression before OR after the pipe |
| [1-9] | Character class — matches any one of: 1-9 |
| \d | Matches any digit (0-9) |
| * | Matches the preceding element zero or more times (greedy) |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| \. | 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 validates negative floating-point numbers, ensuring they have a minus sign and a decimal portion. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
- — Matches the literal minus sign. This is required, making the pattern exclusive to negative numbers.
(?:0|[1-9]\d*)? — An optional non-capturing group for the integer portion before the decimal point. It has two alternatives: a single zero (preventing numbers like -007.5), or a non-zero digit followed by zero or more digits (allowing -3, -42, -100). The entire group is optional to allow forms like -.5.
. — Matches the literal decimal point. The dot is escaped with a backslash because it is a regex metacharacter. The decimal point is mandatory in this pattern.
\d+ — Matches one or more digits after the decimal point. This is the fractional portion and is required, so the pattern does not match integers like -5.
$ — Anchors the match at the end of the string.
No flags are used since this validates a single complete number string.
Negative floating-point numbers are used throughout scientific computing, financial applications, temperature readings, and coordinate systems. Examples include -3.14159, -0.001, -273.15 (absolute zero in Celsius), and -99.99.
This pattern is useful for form validation where negative decimal values are expected, scientific data input, and financial applications handling debits or losses.
Example Test Strings
| Input | Expected |
|---|---|
| -3.14 | Match |
| -0.5 | Match |
| 3.14 | No Match |
| -5 | No Match |
| -.75 | Match |
| -0.001 | Match |
Try It — Interactive Tester
23 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex