import re
regex = re.compile(r"(\+|\-)?(\d{1,})(?:\s|)(ms|millisecond(?:\(s\)|s|)|second(?:\(s\)|s|)|sec|s(?:\s|$)|minute(?:\(s\)|s|)|min|m(?:\s|$)|h(?:\s|$)|hour(?:\(s\)|s|)|h(?:\s|$)|day(?:\(s\)|s|)|d(?:\s|$)|week(?:\(s\)|s|)|w(?:\s|$)|month(?:\(s\)|s|)|mm(?:\s|$)|year(?:\(s\)|s|)|y(?:\s|$))(?:\s|$)?", flags=re.IGNORECASE | re.MULTILINE)
test_str = ("+1ms\n"
"+19milliseconds\n"
"+7 milliseconds\n"
"-1 millisecond\n"
"+7 s\n"
"+8 s\n"
"+4 seconds\n"
"-1 second\n\n"
"+10 minutes\n"
"+1A minutes\n"
"-1 minute\n"
"+1m\n"
"-1m\n"
"+1 m\n"
"-2 m\n"
"1 more time\n"
"1min\n"
"1 min\n\n"
"+19 hours\n"
"-1 hour\n"
"+1h\n"
"-1h\n"
"+1 h\n"
"-2 h\n\n"
"+1 day(s)\n\n"
"+2 week(s)\n\n"
"+3mm\n"
"+3 month(s)\n\n"
"+4 year(s)\n\n"
"1ms\n\n"
"public Myles +1 minute\n"
"Porter Robinson & Madeon - Shelter - Acapella Cover\n"
"haha xd +4 years lol")
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