Tab Character in GO
Match literal tab characters — the regex behind every formatter / linter that yells about indentation.
Try it in the GO tester →Pattern
regexGO
\t (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\t`)
input := ` spaces\there`
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
\t is the escape for the tab character (U+0009). The g flag finds every occurrence. Pair with `replace(re, ' ')` for tabs-to-spaces conversion or use it as a detector to flag mixed indentation.
Examples
Input
spaces\thereMatches
\t
Input
\t\tdouble tabMatches
\t\t
Input
no tabs at allNo match
—