Integer in GO
Matches whole integers, including negative numbers.
Try it in the GO tester →Pattern
regexGO
^-?\d+$Go (RE2) code
goGo
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.
How the pattern works
`^-?` optionally allows a leading minus sign. `\d+` requires one or more digits. `$` anchors to end.
Examples
Input
42Matches
42
Input
-17Matches
-17
Input
3.14No match
—