Hidden File (Dotfile) in GO
Matches Unix-style hidden files (dotfiles) like .gitignore or .env.
Try it in the GO tester →Pattern
regexGO
^\.[^.\s/\\][^/\\]*$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\.[^.\s/\\][^/\\]*$`)
input := `.gitignore`
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
`^\.` requires leading dot. `[^.\s/\\]` requires the next character to not be another dot, whitespace, or slash. `[^/\\]*$` allows any additional non-slash characters.
Examples
Input
.gitignoreMatches
.gitignore
Input
.env.localMatches
.env.local
Input
..No match
—