Integer Regular Expression – Free, Online, Private
Matches an optional minus and an integer with no leading zero.
- 0
- 42
- -7
The pattern
The pattern matches 0, 42, and -7. It rejects 00, 1.5, and +3. A single 0 is allowed. Any other number must start with 1–9. The minus is optional and must be the first character. A plus is not accepted, because a field that allows a plus is a different format. The m flag lets the sample show one candidate per line.
The source is ^-?(?:0|[1-9]\d*)$. The flags on this page are m. i means case-insensitive. m means ^ and $ match at line breaks, which is how the sample can show several candidates. g is not set. The tester finds non-overlapping matches, and without g a pattern with no anchor still reports the matches it walks. The box under the title is a tester with this pattern and the sample already filled in. Edit either one. Nothing you type is uploaded. The same tester, without a preset, is the regex tool.
How to read it
^ and $ anchor a line. -? is the optional sign. (?:0|[1-9]\d*) is either a zero or a non-zero digit followed by any number of digits, including none. The non-capturing group keeps the alternation together so the anchors apply to the whole choice. \d includes digits only. A space fails. An underscore fails. If you want thousands separators, this is the wrong pattern.
What it will not do
The pattern does not limit the number of digits. A 200-digit string matches and will not fit in a 64-bit integer. Check the length or parse with a big-integer type if the value has to be stored. Leading zeros are rejected so that 07 is not quietly read as octal by a later tool. Negative zero is just -0, and it matches, because the minus and the single 0 are both allowed. Decide whether your parser wants that.
A match is not a parsed value. An email pattern does not send mail, a UUID pattern does not allocate an id, and a password pattern does not store a password. When the next step needs a real parser, use one: the URL constructor, a date library, a semver library, or the converters on this site. The expression is the part you can see.
JavaScript, Python, and Go
The JavaScript snippet uses the RegExp constructor so the source does not have to be escaped for a literal slash. The Python snippet uses a normal string with the same escapes JSON would emit, which keeps \d as a regex digit class. Go's snippet uses a raw string. If the pattern contains a lookahead, the Go snippet says so and does not call MustCompile, because RE2 would reject it at runtime.
JavaScriptconst re = new RegExp("^-?(?:0|[1-9]\\d*)$", "m");
const found = text.match(re);
console.log(found ? found[0] : "no match");
Pythonimport re
pattern = re.compile("^-?(?:0|[1-9]\\d*)$", re.MULTILINE)
found = pattern.search(text)
print(found.group(0) if found else "no match")
Gopackage main
import (
"fmt"
"regexp"
)
func main() {
text := ""
re := regexp.MustCompile(`(?m)^-?(?:0|[1-9]\d*)$`)
fmt.Println(re.FindString(text))
}
Frequently asked questions
Does +3 match?
No. Only a leading minus is optional. A plus is rejected.
Does 0 match?
Yes. A single 0 is the first branch. 00 is rejected.
Are decimals matched?
No. 1.5 fails. Use the decimal pattern for that shape.
Is there a length limit?
Not in the pattern. Enforce one if the number has to fit a fixed integer type.