# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^hsla?\(\s*(?<h>[-+]?\d{1,3}(?:\.\d+)?)(deg|grad|rad|turn)?\s*(?:,\s*|\s+)\s*(?<s>[-+]?\d{1,3}(?:\.\d+)?)%\s*(?:,\s*|\s+)\s*(?<l>[-+]?\d{1,3}(?:\.\d+)?)%\s*(?:,\s*|\s+)?(?:\s*\/?\s*(?<alpha>[-+]?[\d.]+%?)\s*)?\)$"
test_str = ("\n"
"hsl(.9, .99%, .999%, .9999)\n"
"hsl(0240, 0100%, 0050%, 01) \n"
"hsl(2400, 1000%, 1000%, 10)\n"
"hsl(-2400.01deg, -1000.5%, -1000.05%, -100)\n"
"hsl(2.40e+2, 1.00e+2%, 5.00e+1%, 1E-3)\n\n"
"hsl(-240, -100%, -50%, -0.1)\n"
"hsl(+240, +100%, +50%, +0.1)\n"
"HSL(240Deg, 100%, 50%)\n"
"hsl(240, 100%, 50%, 10%)\n"
"hsl(150deg 30% 60%)\n"
"hsl(0.3turn 60% 45% .7)\n"
"hsl(0.3grad 60% 45% / .7)\n"
"hsl(0.3rad 60% 45% / .7)\n"
"hsl(12 60% 45% / .7)\n"
"hsl(120, 100%, 50%)\n"
"hsl(0, 0%, 0%)\n"
"hsl(60, 50%, 75%)\n"
"hsl(270, 80%, 60%)\n"
"hsl(200, 20%, 40%)\n"
"hsl(340, 90%, 80%)\n"
"hsla(120, 100%, 50%, 0.5)\n"
"hsla(0, 0%, 0%, 0.2)\n"
"hsla(60, 50%, 75%, 0.8)\n"
"hsla(270, 80%, 60%, 0.4)\n"
"hsla(200, 20%, 40%, 0.6)\n"
"hsla(340, 90%, 80%, 0.1)\n"
"hsl(120 100% 50%)\n"
"hsl(0 0% 0%)\n"
"hsl(60 50% 75%)\n"
"hsl(270 80% 60%)\n"
"hsl(200 20% 40%)\n"
"hsl(340 90% 80%)\n"
"hsla(120 100% 50% / 0.5)\n"
"hsla(0 0% 0% / 0.2)\n"
"hsla(60 50% 75% / 0.8)\n"
"hsla(270 80% 60% / 0.4)\n"
"hsla(200 20% 40% / 0.6)\n"
"hsla(340 90% 80% / 0.1)\n"
"hsl(240, 100%, 50%)\n"
"hsl(240, 100%, 50%, 0.1)\n\n"
"hsl(240,100%,50%,0.1)\n"
"hsl(180deg, 100%, 50%, 0.1)\n"
"hsl(3.14rad, 100%, 50%, 0.1)\n"
"hsl(200grad, 100%, 50%, 0.1)\n"
"hsl(0.5turn, 100%, 50%, 0.1)\n\n"
"hsl(240.5, 99.99%, 49.999%, 0.9999)\n\n\n"
"hsl(240.0, 100.00%, 50.000%, 1.0000)\n\n"
"hsl(240 100% 50%)\n"
"hsl(240 100% 50% / 0.1)\n"
"hsla(240, 100%, 50%)\n"
"hsla(240, 100%, 50%, 0.1)\n")
matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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