DevKits

Numbers

Match an integer

Match positive or negative integers.

Pattern

Regular expression

-?\d+

How it works

Optional minus sign, then one or more digits. Anchor with `^` and `$` if the entire string must be an integer.

Matches

  • 0
  • 42
  • -1
  • 1000000

Non-matches

  • 3.14
  • abc
  • 1e5

Language snippets

JavaScript

const re = /-?\d+/g;

Python

re.findall(r"-?\d+", text)

Go

re := regexp.MustCompile(`-?\d+`)

Java

Pattern.compile("-?\\d+")

Frequently asked questions

What is the regex for integer?

The pattern -?\d+ matches integer. Optional minus sign, then one or more digits. Anchor with `^` and `$` if the entire string must be an integer.

What does this integer pattern match?

It matches strings like 0, 42, -1, but rejects 3.14, abc.

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester