Decimal Number in GO
Matches decimal numbers, including integers and negatives.
Try it in the GO tester →Pattern
regexGO
^-?\d+(\.\d+)?$Go (RE2) code
goGo
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.
How the pattern works
`^-?\d+` matches optional sign and integer part. `(\.\d+)?` optionally matches a decimal point and fractional digits.
Examples
Input
3.14Matches
3.14
Input
-0.001Matches
-0.001
Input
42Matches
42