I'm experienced with regular expressions but new to using them in Python (Python 3 specifically). The behavior of the below script is strange to me. The goal is to match hex colors in my multiline string like #FfFdF8 but not match #BED since that's a CSS specifier.
Anyway the script is working overall, except that the number sign (#) itself isn't being included in the strings returned by findall(). So it will return, for example, "aef" instead of "#aef". I can't think what I might be overlooking, since # isn't a special regex character and findall() is supposed to return all matches as strings. Any idea?
import re
mystr = """
#BED
{
color: #FfFdF8; background-color:#aef;
font-size: 123px;
}
"""
lines = mystr.split('\n')
p = re.compile(r'#([a-zA-Z0-9]{3}|[a-zA-Z0-9]{6})\b')
for line in lines:
if (line[0:4] == " "):
matches = p.findall(line)
for m in matches:
print(m)
Actual output:
FfFdF8
aef
Desired output:
#FfFdF8
#aef