Unix File Path in GO
Matches absolute Unix/Linux file paths.
Try it in the GO tester →Pattern
regexGO
^(/[^/\0]+)+/?$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(/[^/\0]+)+/?$`)
input := `/home/user/documents`
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
`^(/[^/\0]+)+` matches one or more path segments beginning with `/`, excluding null and additional slashes within segments. `/?$` allows optional trailing slash.
Examples
Input
/home/user/documentsMatches
/home/user/documents
Input
/var/log/Matches
/var/log/
Input
relative/pathNo match
—