Skip to content

Instantly share code, notes, and snippets.

@unnonouno
Created April 27, 2021 04:50
Show Gist options
  • Save unnonouno/9ae67c92f71300df3ab980f610aef712 to your computer and use it in GitHub Desktop.
Save unnonouno/9ae67c92f71300df3ab980f610aef712 to your computer and use it in GitHub Desktop.
Structured Regexp
digits = Capture(r"\d+", int)
date = Seq(
(digits, "/", digits, "/", digits),
lambda year, _1, month, _2, day: datetime.date(year, month, day),
)
date_time = Seq(
(digits, "/", digits, "/", digits, " ", digits, ":", digits),
lambda year, _1, month, _2, day, _3, hour, _4, minute: datetime.datetime(year, month, day, hour, minute),
)
span = Seq(((date | date_time), "-", (date | date_time)), lambda begin, _, end: (begin, end))
spans = RepeatEx(span, delimiter=", *")
match = spans.match("2020/08/01 9:00-2020/09/10 10:30, 2020/10/01-2020/10/30")
print(match)
[(datetime.datetime(2020, 8, 1, 9, 0), datetime.datetime(2020, 9, 10, 10, 30)), (datetime.date(2020, 10, 1), datetime.date(2020, 10, 30))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment