Go (RE2)

Hex Color Code in GO

Match CSS hex color codes in both 3-digit (#RGB) and 6-digit (#RRGGBB) formats.

Try it in the GO tester →

Pattern

regexGO
#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b`)
	input := `#ff5733`
	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 a # character followed by exactly 6 or exactly 3 hexadecimal digits. The \b word boundary prevents partial matches inside longer strings.

Examples

Input

#ff5733

Matches

  • #ff5733

Input

#abc

Matches

  • #abc

Input

#FFFFFF

Matches

  • #FFFFFF

Same pattern, other engines

← Back to Hex Color Code overview (all engines)