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 hexa characters, or 3 groups of one hexa character each.
To take advantage of the output without much complexity, you can concatenate the groups 2 and 5, 3 and 6, 4 and 7 respectively, because only one of each pair will be filled.
#?(([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})|([0-9a-f]{3}))
This is the "original" of the question, with 3 or 1 group.
Simple and direct version without regex:
For this kind of trouble, I think regex although it appears to be shorter, it is of an unnecessary complexity, both to be processed and to be debugged, so I decided to put this basic example using "traditional programming".
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 )
Exit:
red ba
green cc
blue 01
To adapt to #abc go out in one group, just change what is inside the if
initial.
A if which checks the length of string does not solve no?
– Oralista de Sistemas