Hashtag in GO
Match hashtags (# followed by word characters) in social media posts, including accented Latin characters.
Try it in the GO tester →Pattern
regexGO
#([\w\u00C0-\u024F]+) (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`#([\w\u00C0-\u024F]+)`)
input := `Loving #JavaScript and #regex!`
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
# matches the literal hash. The capturing group ([\w\u00C0-\u024F]+) matches one or more word characters (letters, digits, underscore) plus Latin Extended Unicode range for accented characters like #café or #naïve.
Examples
Input
Loving #JavaScript and #regex!Matches
#JavaScript#regex
Input
Post tagged #café and #naïveMatches
#café#naïve
Input
No hashtags hereNo match
—