Percentage in GO
Matches percentage values with optional decimal and a trailing % sign.
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 := `95%`
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 integer part. `(\.\d+)?` optional decimals. `%$` requires a trailing percent sign at end.
Examples
Input
95%Matches
95%
Input
99.9%Matches
99.9%
Input
100No match
—