Integer
Matches whole integers, including negative numbers.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("^-?\\d+$", "");
const input = "42";
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+$")
input_text = "42"
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+$`)
input := `42`
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+$Raw source: ^-?\d+$
How it works
Examples
Input
42Matches
42
Input
-17Matches
-17
Input
3.14No match
—Common use cases
- •Form validation
- •ID parsing
- •Configuration values
Related patterns
Decimal Number
NumbersMatches decimal numbers, including integers and negatives.
Float / Scientific Number
NumbersMatch floating-point and scientific-notation numbers including `1.5`, `.25`, `1e10`, `-3.14E-2`.
Scientific Notation
NumbersMatches numbers in scientific/exponential notation (e.g., 1.5e10).
Binary Number Literal
NumbersMatch binary number literals like `0b1010` or `0B11110000`.
Related concepts
Anchors: ^ and $ Explained
ConceptAnchors match positions, not characters. ^ asserts the start of the string (or line, with the m flag) and $ asserts the end.
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.
Quantifiers: *, +, ?, and {n,m}
ConceptQuantifiers repeat the preceding element. * is 0 or more, + is 1 or more, ? is 0 or 1. {n}, {n,}, and {n,m} give exact counts and ranges.
How to Match Digits in Regex
How-toUse \d for any digit, [0-9] for ASCII digits only, or {n} to match a specific count of digits. Combine with anchors for whole-string validation.