MAC Address in GO
Match 48-bit MAC addresses written with colon or hyphen separators (e.g. 00:1A:2B:3C:4D:5E).
Try it in the GO tester →Pattern
regexGO
(?:[0-9A-Fa-f]{2}[:\-]){5}[0-9A-Fa-f]{2} (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:[0-9A-Fa-f]{2}[:\-]){5}[0-9A-Fa-f]{2}`)
input := `00:1A:2B:3C:4D:5E`
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
Five pairs of hex digits each followed by : or -, then a final hex pair. Case-insensitive by character class to handle both upper and lower hex digits.
Examples
Input
00:1A:2B:3C:4D:5EMatches
00:1A:2B:3C:4D:5E
Input
AA-BB-CC-DD-EE-FFMatches
AA-BB-CC-DD-EE-FF
Input
not a macNo match
—