I am a beginner with Regex so I keep practicing by solving all the exercises I can find. In one of them, I need to extract all the Hex codes from a HTML source code, using Regex and Python. According to the exercise, the rules for spotting a Hex code are:
- It starts with #
- It has 3 or 6 digits
- Each digit is in the range of 0-F (the string is case insensitive)
The sample input is this:
#BED { color: #FfFdF8; background-color:#aef; font-size: 123px; background: -webkit-linear-gradient(top, #f9f9f9, #fff); } #Cab { background-color: #ABC; border: 2px dashed #fff; }
The desired output is:
#FfFdF8 #aef #f9f9f9 #fff #ABC #fff
#BED and #Cab are to be omitted, because they are not Hex colors.
I tried this code, to solve the problem:
import re
text = """
#BED
{
color: #FfFdF8; background-color:#aef;
font-size: 123px;
background: -webkit-linear-gradient(top, #f9f9f9, #fff);
}
#Cab
{
background-color: #ABC;
border: 2px dashed #fff;
} """
r = re.compile(r'#[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}')
a = r.findall(text)
print(a)
Obtained output:
['#BED', '#FfF', '#aef', '#f9f', '#fff', '#Cab', '#ABC', '#fff']
It works fine, except that it doesn't catch the 6-digit codes and it doesn't eliminate the two tags that actually are not Hex color codes.
What am I mistaking? I looked at other attempts, but they didn't provide the correct answer. I am using Python 3.7.4 and the latest version of PyCharm.

#BEDand#CABare valid hex colors.#BEDand#CABare not colors in that example.