Skip to content

Instantly share code, notes, and snippets.

@EvanK
Last active March 11, 2024 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvanK/3b75584da4c4d0f6447ae083a28c1b17 to your computer and use it in GitHub Desktop.
Save EvanK/3b75584da4c4d0f6447ae083a28c1b17 to your computer and use it in GitHub Desktop.
A work in progress PCRE for matching [AWS EventBridge scheduling expressions](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cron-expressions.html). See it in action [on RegExr](https://regexr.com/7t6id).
# the whole enchilada...
/
^
(
rate\(
(
(
1[ ]+(hour|minute|day))
|
([0-9]+[ ]+(hours|minutes|days)
)
)
\)
)
|
(
cron\(
# minutes field
(?:
(?:
(?:
[0-5]?
[0-9]
)
|
[*]
)
[,/-]?
){1,}
[ ]+
# hours field
(?:
(?:
(?:
[0-2]?
[0-9]
)
|
[*]
)
[,/-]?
){1,}
[ ]+
# day of month field
(?:
(?:
(?:
[1-3]?
[0-9]
)
|
[LW*?]
)
[,/-]?
){1,}
[ ]+
# month field
(?:
(?:
(?:
[1]?
[0-9]
)
|
(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)
|
[*]
)
[,/-]?
){1,}
[ ]+
# day of week field
(?:
(?:
[1-7]
|
(?:SUN|MON|TUE|WED|THU|FRI|SAT)
|
[L*?]
)
[#,/-]?
){1,}
[ ]+
# year field
(?:
(?:
(?:
[12]
[0-9]{3}
)
|
[*]
)
[,/-]?
){1,}
\)
)
$
/x;
# each cron field broken out for clarity and debugging purposes...
/
# minutes field
(?:
(?:
(?:
[0-5]?
[0-9]
)
|
[*]
)
[,/-]?
){1,}
/x;
/
# hours field
(?:
(?:
(?:
[0-2]?
[0-9]
)
|
[*]
)
[,/-]?
){1,}
/x;
/
# day of month field
(?:
(?:
(?:
[1-3]?
[0-9]
)
|
[LW*?]
)
[,/-]?
){1,}
/x;
/
# month field
(?:
(?:
(?:
[1]?
[0-9]
)
|
(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)
|
[*]
)
[,/-]?
){1,}
/x;
/
# day of week field
(?:
(?:
[1-7]
|
(?:SUN|MON|TUE|WED|THU|FRI|SAT)
|
[L*?]
)
[#,/-]?
){1,}
/x;
/
# year field
(?:
(?:
(?:
[12]
[0-9]{3}
)
|
[*]
)
[,/-]?
){1,}
/x;
# Field Values Wildcards
# Minutes 0-59 , - * /
# Hours 0-23 , - * /
# Day-of-month 1-31 , - * ? / L W
# Month 1-12 or JAN-DEC , - * /
# Day-of-week 1-7 or SUN-SAT , - * ? L #
# Year 1970-2199 , - * /
# Additional restrictions (not yet implemented)
# 1. Day-of-week, if you use a '#' character, you can define only one expression in the day-of-week field. For example, "3#1,6#3" is not valid because it is interpreted as two expressions.
# 2. You can't specify the Day-of-month and Day-of-week fields in the same cron expression. If you specify a value or a * (asterisk) in one of the fields, you must use a ? (question mark) in the other.
# ^^ May be able to implement these with look-(behind|ahead)? ...this one's gonna be complex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment