IPv6 Address in GO
Match full (non-compressed) IPv6 addresses written as eight colon-separated hextets.
Try it in the GO tester →Pattern
regexGO
(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4} (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}`)
input := `2001:0db8:85a3:0000:0000:8a2e:0370:7334`
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
Seven groups of 1–4 hex digits each followed by a colon, then a final hextet. Does not cover :: shorthand or IPv4-mapped addresses — use a more complex pattern if you need those forms.
Examples
Input
2001:0db8:85a3:0000:0000:8a2e:0370:7334Matches
2001:0db8:85a3:0000:0000:8a2e:0370:7334
Input
::1No match
—