RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a negative number

/^-\d+(\.\d+)?$/

The minus is required rather than optional, which is the whole difference from the general decimal pattern. Note that -0 matches: it is a well-formed negative zero, and if that matters to your rule you filter it in code, not here.

What each part does

^-the string must begin with a minus sign
\d+(\.\d+)?an ordinary decimal number

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
-1matches-1
-0.25matches-0.25
-0matches-0
1no match
+1no match
- 1no match
−5no match

The mistake to avoid

The Unicode minus sign U+2212 (−) is not the ASCII hyphen. Text pasted from a document or a spreadsheet frequently uses it, and it will not match.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers