Windows File Path in GO
Matches absolute Windows file paths (e.g., C:\Users\file.txt).
Try it in the GO tester →Pattern
regexGO
^[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`)
input := `C:\Users\John\file.txt`
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
`^[A-Za-z]:\\` matches the drive letter and root backslash. `(?:[^\\/:*?"<>|\r\n]+\\)*` matches directory segments. Final segment (the file or folder name) is allowed to be empty.
Examples
Input
C:\Users\John\file.txtMatches
C:\Users\John\file.txt
Input
D:\Matches
D:\
Input
/unix/pathNo match
—