Alphanumeric Only in GO
Match strings containing only letters (A–Z, a–z) and digits (0–9), with no spaces or special characters.
Try it in the GO tester →Pattern
regexGO
^[a-zA-Z0-9]+$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z0-9]+$`)
input := `Hello123`
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
^ and $ anchor to the full string. [a-zA-Z0-9]+ requires one or more characters from the combined letter and digit set. Any space, punctuation, or symbol will cause the match to fail.
Examples
Input
Hello123Matches
Hello123
Input
abcMatches
abc
Input
has spaceNo match
—