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.
| Input | Result | Matched |
|---|---|---|
| -1 | matches | -1 |
| -0.25 | matches | -0.25 |
| -0 | matches | -0 |
| 1 | no match | — |
| +1 | no match | — |
| - 1 | no match | — |
| −5 | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID