~/devtools / regex / number
tool::regex-guide

regex number

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

Number (Integer / Decimal) Validation

ad · 728×90
Regex Pattern
/^-?\d+(\.\d+)?$/
Flags:none
Pattern Breakdown
^Start of string
-?Optional minus sign (for negative numbers)
\d+One or more digits (integer part)
(\.\d+)?Optional decimal point and digits
$End of string
Matching Examples
✓42
✓-7
✓3.14
✓-0.001
Non-Matching Examples
✗3.14.15
✗1,000
✗NaN
✗1e10

Validates both integers and decimal numbers including negative values. Composed of an optional minus sign, integer part, and optional decimal part. Numbers with commas (1,000) or exponential notation (1e10) require separate patterns.

Language Usage

JavaScript— Use RegExp object or literal syntax
const pattern = /^-?\d+(\.\d+)?$/;

// Test a string
console.log(pattern.test("42")); // true
console.log(pattern.test("3.14.15")); // false

// Match and extract
const result = "42".match(pattern);
Python— Use the re module
import re

pattern = r"^-?\d+(\.\d+)?$"

# Test a string
if re.fullmatch(pattern, "42"):
    print("Valid")

# Find all matches in text
matches = re.findall(pattern, text)
Java— Use java.util.regex package
import java.util.regex.*;

String pattern = "^-?\\d+(\\.\\d+)?$";
Pattern p = Pattern.compile(pattern);

Matcher m = p.matcher("42");
boolean isValid = m.matches(); // true

// Find occurrences
Matcher finder = p.matcher(inputText);
while (finder.find()) {
    System.out.println(finder.group());
}

Common Use Cases

  • ▸Validate price or amount input fields
  • ▸Check coordinate data format (latitude, longitude)
  • ▸Parse input for math calculators
  • ▸Validate numeric values in config files
  • ▸Clean numeric columns in CSV files

Related Patterns

email/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-…/i→phone/^\+?[\d\s\-\(\)]{7,15}$/→url/^https?:\/\/[^\s\/$.?#].[^\s]*$/i→ip-address/^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.…/→
ad · 300×250
← Back to Regex Tester
// related tools
⏱
Cron Expression Generator
Build and parse cron expressions visually. Generate human-readable descriptions and preview next execution times.
jwt
JWT Decoder
Decode and inspect JWT tokens. View header, payload, and signature details.
◑
Color Converter
Convert colors between HEX, RGB, HSL, and more. Pick colors visually.
ts
Timestamp Converter
Convert Unix timestamps to human-readable dates. Supports ms/s, UTC/local, and relative time.