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

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
-Matches the literal character '-'
(?:Start of non-capturing group
0Matches the literal character '0'
|Alternation — matches the expression before OR after the pipe
[1-9]Character class — matches any one of: 1-9
\dMatches 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
\dMatches 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

InputExpected
-3.14Match
-0.5Match
3.14No Match
-5No Match
-.75Match
-0.001Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 23 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →