import re
regex = re.compile(r"""
# SETTINGS
(?<ANCHORED>){1}
(?<NO_SIGN_DUP>){1}
(?<PYTHON_POW>){1}
(?<COMMENTS>){0}
(?(ANCHORED)^(?&s)*)
(?(DEFINE)
(?<s>(?(COMMENTS)(?:\s+|\/\*[\s\S]*?\*\/|\/\/.*|\#.*)|\s+))
(?<sign>(?(NO_SIGN_DUP)(?<=(?<_>[\s\S]))(?!(?P=_)))[-+])
(?<base>\d+(?:\.\d*)?|\d*\.\d+)
(?<exp>(?:[eE][+-]?\d+))
(?<num>(?&sign)?(?&base)(?&exp)?(?!\w))
(?<bracketed>\((?&s)*(?&expr)(?&s)*\))
(?<atom>(?&num)|(?&bracketed))
(?<pow>(?&atom)(?:(?&s)*(?(PYTHON_POW)(?:\^|\*\*)|\^)(?&s)*(?&pow))*)
(?<mul>(?&pow)(?:(?&s)*[*\/](?&s)*(?&mul))*)
(?<add>(?&mul)(?:(?&s)*[+-](?&s)*(?&add))*)
(?<expr>(?&add))
)
(?&expr)
(?(ANCHORED)(?&s)*$)
""", flags=re.VERBOSE | re.MULTILINE)
test_str = ("0\n"
"::\n"
"1\n"
"::\n"
"+1\n"
"::\n"
"-1\n"
"::\n"
"1.e-1\n"
"::\n"
"1e1\n"
"::\n"
".1e+1\n"
"::\n"
"-6*((2**(2+4))/3)\n"
"::\n"
"0*0\n"
"::\n"
"0+0\n"
"::\n"
"0/0\n"
"::\n"
"0^0\n"
"::\n"
"0**0\n"
"::\n"
"0-0\n"
"::\n"
"5 - -2\n"
"::\n"
"5- -2\n"
"::\n"
"5+-2\n"
"::\n"
".1\n"
"::\n"
"1.\n"
"::\n"
"1.1\n"
"::\n"
" -2\n"
"::\n"
"5 * (2 + 5 /* comment */ // )\n"
" - 1)\n\n"
"INVALID: // Separated by :: to prevent parsing multiple examples\n"
".\n"
"::\n"
"1e\n"
"::\n"
"1e+\n"
"::\n"
"1e-\n"
"::\n"
"e2\n"
"::\n"
"+\n"
"::\n"
"-\n"
"::\n"
"5 --2\n\n")
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