Python Package Name (PEP 508) in GO
Validate Python distribution package names per PEP 508: letters, digits, dots, underscores, hyphens.
Try it in the GO tester →Pattern
regexGO
^[A-Za-z0-9](?:[A-Za-z0-9._\-]*[A-Za-z0-9])?$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9._\-]*[A-Za-z0-9])?$`)
input := `requests`
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
Starts and ends with alphanumeric. Allows `_`, `.`, `-` in the middle. The single-char form (just an alphanumeric) is also valid. PEP 508 / PEP 503 normalize all of `_`, `.`, `-` to a single `-` for matching, but the underlying name on PyPI may use any.
Examples
Input
requestsMatches
requests
Input
django-rest-frameworkMatches
django-rest-framework
Input
_invalidNo match
—