Created
April 27, 2021 04:50
-
-
Save unnonouno/9ae67c92f71300df3ab980f610aef712 to your computer and use it in GitHub Desktop.
Structured Regexp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[(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