Skip to content

Instantly share code, notes, and snippets.

@e-dreyer
Created August 13, 2023 15:46
Show Gist options
  • Save e-dreyer/ce0f5e8c51d6454f91901f78f9e04b77 to your computer and use it in GitHub Desktop.
Save e-dreyer/ce0f5e8c51d6454f91901f78f9e04b77 to your computer and use it in GitHub Desktop.
RemindMe bot regex testing
import re
pattern = r'(?i)@remindMe\s*(?:(?:(\d+)\s*years?)?\s*)?(?:(\d+)\s*months?)?\s*(?:(\d+)\s*weeks?)?\s*(?:(\d+)\s*days?)?\s*(?:(\d+)\s*hours?)?\s*(?:(\d+)\s*minutes?)?'
tests: list[str] = list([
"@remindMe 3 years 2 months 1 weeks 1 day",
"@remindMe 6 months 2 days",
"@remindMe 7 months 1 day",
"@remindMe 2 weeks",
"@remindMe 1 day",
"@remindMe 2 days",
"@remindMe 1 hour",
"@remindMe 2 weeks 3 hours"
])
for test in tests:
print(f"test: {test}")
matches = re.search(pattern, test)
if matches:
years, months, weeks, days, hours, minutes = matches.groups()
print(f"Years: {years}, Months: {months}, Weeks: {weeks}, Days: {days}, Hours: {hours}, Minutes: {minutes}")
else:
print("No match found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment