I am trying to check a string for a pattern which its length can be either 3 or 6; not the values between them.
This is the string:
color: #FfFdF8; background-color:#aef;
I want to get all sub-strings starting with # followed by a hex code, if they have the length of 3 or 6 and are not located at the beginning of the string; in this case both #FfFdF8 and #aef should be returned.
I have written this pattern:
r'^(?!#).+(#[a-fA-F0-9]{6}).*|^(?!#).+(#[a-fA-F0-9]{3}).*'
But it gave me [('#FfFdF8', '')] as the result of re.findall.
if not s.startswith('#'): results = re.findall(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?\b', s)