import re
regex = re.compile(r"^\s*?((?:virtual\s*|const\s*|friend\s*){0,3})\s*([\w]*?)\s*\w+\s*::\s*([\w]+)\s*(\(.*\))\s*(const)?\s*[{|:]", flags=re.MULTILINE)
test_str = ("#include <iostream>\n"
"#include <iomanip>\n"
"#include \"Time.h\"\n"
"using namespace std;\n\n"
"Time :: Time(const int h, const int m, const int s) \n"
" : hour(h), minute (m), second(s)\n"
"{}\n\n"
"virtual const void Time :: setTime(const int h, const int m, const int s) \n"
"{\n"
" hour = h;\n"
" minute = m;\n"
" second = s; \n"
"} \n"
" \n"
"std::string Time :: print() const\n"
"{\n"
" cout << setw(2) << setfill('0') << hour << \":\"\n"
" << setw(2) << setfill('0') << minute << \":\"\n"
" << setw(2) << setfill('0') << second << \"\\n\"; \n"
" \n"
"}\n"
" \n"
"bool Time :: equals(const Time &otherTime)\n"
"{\n"
" if(hour == otherTime.hour \n"
" && minute == otherTime.minute \n"
" && second == otherTime.second)\n"
" return true;\n"
" else\n"
" return false;\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