import re
regex = re.compile(r"(?:(?:rgb\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\))|(?:rgba\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3}),\s?([0-1]\.0|0\.[0-9])\)))", flags=re.MULTILINE)
test_str = ("rgb(0, 0, 0)\n"
"rgb(0,0,0)\n"
"rgb(255, 255, 255)\n"
"rgba(255, 255, 255, 1)\n"
"rgba(255, 255, 255,1)\n"
"rgba(255, 255, 255, 0)\n"
"rgba(255, 255, 255, 0.9)\n"
"rgba(255, 255, 255, 0.0)\n"
"rgba(255, 255, 255, 1.0)\n"
"rgba(255, 255, 255, 1.1)\n"
"rgba(255, 255, 255, 666)\n"
"rgba(255, 255, 255ab)\n"
"rgb(255, 255, 255, 344)\n\n"
"#p1 {background-color: rgba(255, 0, 0, 0.3);} /* red with opacity */\n"
"#p2 {background-color: rgba(0, 255, 0, 0.3);} /* green with opacity */\n"
"#p3 {background-color: rgba(0, 0, 255, 0.3);} /* blue with opacity */")
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