Back to Home

Negative Integer Validation Regex

CronOS Team
regexintegervalidationtutorialnumbernegative

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

Negative Integer Validation Regex

Validate negative integers (negative whole numbers) using a simple regex pattern with minus sign.

Pattern Breakdown

regex
^-\d+$

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
-Minus signLiteral minus (required)
\d+One or more digitsInteger digits
$End anchorEnsures match to string end

Character Classes

  • - - Literal minus sign
  • \d - Any digit (0-9)
  • + - Quantifier: one or more occurrences

Examples

Valid:

  • -1
  • -123
  • -999999
  • -1000000

Invalid:

  • 0 (zero is not negative)
  • 5 (positive number)
  • -0 (negative zero, typically not considered negative)
  • --5 (double minus)
  • -12.5 (decimal)
  • -abc (non-numeric)

Implementation

JavaScript

javascript
const negativeIntRegex = /^-\d+$/;
negativeIntRegex.test('-123'); // true
negativeIntRegex.test('-1'); // true
negativeIntRegex.test('5'); // false (positive)
negativeIntRegex.test('-12.5'); // false (decimal)
negativeIntRegex.test('0'); // false (zero)

Python

python
import re
negative_int_regex = r'^-\d+$'
bool(re.match(negative_int_regex, '-123'))  # True
bool(re.match(negative_int_regex, '-1'))  # True
bool(re.match(negative_int_regex, '5'))  # False (positive)
bool(re.match(negative_int_regex, '-12.5'))  # False (decimal)

Go

go
negativeIntRegex := regexp.MustCompile(`^-\d+$`)
negativeIntRegex.MatchString("-123") // true
negativeIntRegex.MatchString("-1") // true
negativeIntRegex.MatchString("5") // false (positive)
negativeIntRegex.MatchString("-12.5") // false (decimal)

Limitations

  1. Includes -0: Matches -0 (may want to exclude this)
  2. No size limit: Doesn't enforce maximum/minimum value
  3. String validation: Validates string format, not actual integer type
  4. No leading zeros: Accepts -0123 (may want to prevent this)
  5. No scientific notation: Doesn't support -1e5 format

When to Use

  • Negative integer input validation
  • Temperature below zero
  • Debt/credit calculations
  • When you need to ensure negative numbers only
  • Form input validation for negative values

For production, consider:

  • Excluding -0 if needed: ^-(0*[1-9]\d*)$
  • Preventing leading zeros: ^-(0|[1-9]\d*)$
  • Adding value range validation
  • Converting to integer type after validation
  • Handling edge cases for very large negative numbers

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern