import re
regex = re.compile(r"(\w+\s+\w+\s+[\d:\s]+[A-Z]{3}\s+\d{4})[\s\S]+?((?:\w+)\s+(?:[0-9.]+))\s+(?:((?:\w+)\s*(?:[0-9.]+)))?\s+(?:((?:\w+)\s*(?:[0-9.]+)))?\s*(?:((?:\w+)\s+(?:[0-9.]+)))?\s*(?:((?:\w+)\s*(?:[0-9.]+)))?")
test_str = ("Mon Feb 1 09:12:41 GMT 2016\n\n"
"Rotating fax log files:\n\n"
"Doing login accounting:\n"
" total 688.31\n"
" example 401.12\n"
" _mbsetupuser 287.10\n"
" root 0.05\n"
" admin 0.04\n\n"
"-- End of monthly output --\n\n"
"Tue Feb 16 14:27:21 GMT 2016\n\n"
"Rotating fax log files:\n\n"
"Doing login accounting:\n"
" total 0.00\n\n"
"-- End of monthly output --\n\n"
"Thu Mar 3 09:37:31 GMT 2016\n\n"
"Rotating fax log files:\n\n"
"Doing login accounting:\n"
" total 377.92\n"
" example 377.92\n\n"
"-- End of monthly output --")
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