import re
regex = re.compile(r"((?:#|0x)(?:[a-f0-9]{3}|[a-f0-9]{6})\b|(?:rgb|hsl)a?\([^\)]*\))", flags=re.UNICODE | re.IGNORECASE)
test_str = ("yo #5E81AC and there rgb(50, 186, 86) or even #1364e7 and rgb(242, 48, 84)\n\n\n\n"
"style=\\\"color: rgb(255,0,24)\\\"\n"
"style=\\\"color: rgb(255, 0, 24)\\\"\n"
"style=\\\"color: rgba(255, 0, 24, .5)\\\"\n"
"style=\\\"color: hsla(170, 23%, 25%, 0.2 )\\\"\n"
"style=\\\"color: #fff\\\"\n"
"style=\\\"color: #f2ewq\\\" this isn\\'t a color\n"
"style=\\\"color: #F2F2F2\\\"\n"
"style=\\\"color: 0x00ffff\\\" This is a color with the `#` escaped")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html