Decimal Number
Matches decimal numbers, including integers and negatives.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("^-?\\d+(\\.\\d+)?$", "");
const input = "3.14";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"^-?\d+(\.\d+)?$")
input_text = "3.14"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^-?\d+(\.\d+)?$`)
input := `3.14`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
^-?\d+(\.\d+)?$Raw source: ^-?\d+(\.\d+)?$
How it works
Examples
Input
3.14Matches
3.14
Input
-0.001Matches
-0.001
Input
42Matches
42
Common use cases
- •Price input
- •Scientific data
- •Form validation
Related patterns
Float / Scientific Number
NumbersMatch floating-point and scientific-notation numbers including `1.5`, `.25`, `1e10`, `-3.14E-2`.
Integer
NumbersMatches whole integers, including negative numbers.
Binary Number Literal
NumbersMatch binary number literals like `0b1010` or `0B11110000`.
Hexadecimal Number Literal
NumbersMatch hexadecimal number literals like `0xFF`, `0x1A2B`, or `0XdeadBeef`.
Related concepts
Character Classes: \d, \w, \s and Their Negations
ConceptShorthand character classes match broad categories of characters. \d is a digit, \w is a word character, \s is whitespace. Uppercase negates.
Custom Character Sets: [abc], [a-z], [^abc]
ConceptSquare brackets build a custom character class. [abc] matches any of a, b, or c. [a-z] is a range. A leading ^ negates the set.