import re
regex = re.compile(r"""
\s*
(
(?<type>int(?:\s+long|\s+short)?|double(?:\s+long)?|
long(?:\s+int|\s+double)?|short(?:\s+int)?|float|char)
|
(?<error>\w+|\w+\s*\,)
)
\s+
(?:
(?:
(?:
(?<error>int|double|float|char|long|short|[^,;a-zA-Z_].+?|[^,;]+?(?:\s+\w+?)+)
|
(?<id>[a-zA-Z_][a-zA-Z_0-9]*)
)
(?:\s*\[\s*[1-9]\d*\s*\])*
)
\s*
(?:\,\s*|(?=\;))
)+
\s*\;\s*
""", flags=re.MULTILINE | re.VERBOSE)
test_str = ("int \n"
" abv, cde, doub1lel,ded, cto, int1;\n"
"float ng23423, _ga45, \n"
"astories; \n"
"int af , f;\n"
"int a, b, c;int short alt ;\n"
"long double fgd [ 1 ], [1] , wef ;\n"
"char _a[1], fi [\n"
" 3 \n"
" ] [ 10] [ 4 ] ,g[1];int qw;")
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