To answer the question, here is the version with regex :
#?(([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})|([0-9a-f])([0-9a-f])([0-9a-f]))
This Regex separates by 3 groups of two hex characters, or 3 groups of one hex character each.
To use the output without too much complexity, you can concatenate groups 2 and 5, 3 and 6, 4 and 7 respectively, as only one of each pair is filled.
#?(([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})|([0-9a-f]{3}))
This is the "original" question, with 3 or 1 group.
Simple and straightforward version without regex :
For this type of problem, I think regex although it seems to be shorter, is of unnecessary complexity, both to be processed and to be debugged, so I decided to put this basic example using programming "traditional".
It works with or without #
at the beginning, and with 3 or 6 digits.
color = "#bacc01"
if color[0]=="#":
color = color[1:]
if len(color) == 3:
r = color[0]
g = color[1]
b = color[2]
else:
r = color[0:2]
g = color[2:4]
b = color[4:6]
print( "red ", r )
print( "green", g )
print( "blue ", b )
Output:
red ba
green cc
blue 01
To adapt to #abc you can leave in a group only, just change whatever is inside the if
inicial.