Last active
August 29, 2015 14:08
-
-
Save ThisIsMissEm/92159d71e9ede1415154 to your computer and use it in GitHub Desktop.
Can you simplify this Regular Expression? Must be JavaScript compatible.
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
/(MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY|SUNDAY)(\-(MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY|SUNDAY)|( AND (MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY|SUNDAY)))? \/ (1AM|2AM|3AM|4AM|5AM|6AM|7AM|8AM|9AM|10AM|11AM|MIDDAY|1PM|2PM|3PM|4PM|5PM|6PM|7PM|8PM|9PM|10PM|11PM|MIDNIGHT)\-(1AM|2AM|3AM|4AM|5AM|6AM|7AM|8AM|9AM|10AM|11AM|MIDDAY|1PM|2PM|3PM|4PM|5PM|6PM|7PM|8PM|9PM|10PM|11PM|MIDNIGHT) \/ (WEEKLY|BI-WEEKLY|MONTHLY|BI-MONTHLY)/ |
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
MONDAY / 9AM-10PM / WEEKLY | |
MONDAY-THURSDAY / 9AM-10PM / WEEKLY | |
TUESDAY AND THURSDAY / 9AM-10PM / WEEKLY | |
MONDAY / 9AM-MIDDAY / BI-WEEKLY | |
MONDAY-THURSDAY / 11PM-MIDNIGHT / MONTHLY | |
TUESDAY AND THURSDAY / MIDDAY-3PM / BI-MONTHLY | |
THURSDAY / MIDNIGHT-2AM / MONTHLY |
If by "simplify" you meant ⛳
/(MON|TUES|WEDNES|THURS|FRI|SATUR|SUN)DAY(\-(MON|TUES|WEDNES|THURS|FRI|SATUR|SUN)DAY|( AND (MON|TUES|WEDNES|THURS|FRI|SATUR|SUN)DAY))? \/ ((([1-9]|1[01])[AP]M)|MID(NIGHT|DAY)?)\-((([1-9]|1[01])[AP]M)|MID(NIGHT|DAY)?) \/ ((BI-)?(WEEK|MONTH)LY)/
This one lets you capture the separator between the two days and doesn't repeat the days 3 times:
((?:MON|TUES|WEDNES|THURS|FRI|SATUR|SUN)DAY)(-((?:MON|TUES|WEDNES|THURS|FRI|SATUR|SUN)DAY)| AND ((?:MON|TUES|WEDNES|THURS|FRI|SATUR|SUN)DAY))? \/ ((?:[1-9]|1[01])[AP]M|MID(?:DAY|NIGHT))-((?:[1-9]|1[01])[AP]M|MID(?:DAY|NIGHT)) \/ ((?:BI-)?(?:WEEKLY|MONTHLY))
Created and tested with:
var samples = [
'MONDAY / 9AM-10PM / WEEKLY'
, 'MONDAY-THURSDAY / 9AM-10PM / WEEKLY'
, 'TUESDAY AND THURSDAY / 9AM-10PM / WEEKLY'
, 'MONDAY / 9AM-MIDDAY / BI-WEEKLY'
, 'MONDAY-THURSDAY / 11PM-MIDNIGHT / MONTHLY'
, 'TUESDAY AND THURSDAY / MIDDAY-3PM / BI-MONTHLY'
, 'THURSDAY / MIDNIGHT-2AM / MONTHLY'
]
var parts = {
day: '((?:MON|TUES|WEDNES|THURS|FRI|SATUR|SUN)DAY)'
, time: '((?:[1-9]|1[01])[AP]M|MID(?:DAY|NIGHT))'
, recur: '((?:BI-)?(?:WEEKLY|MONTHLY))'
}
var template = '^{day}(?:(-| AND ){day})? / {time}-{time} / {recur}$'
var re = new RegExp(template.replace(/{(\w+)}/g, function(match, part) { return parts[part] }))
console.log('Testing: /' + re.source + '/')
samples.forEach(function(input) {
console.log(input + ': ' + re.test(input))
})
var fields = ['day1', 'rangeType', 'day2', 'from', 'to', 'recurrence']
function parse(input) {
var match = re.exec(input)
if (!match) { return null }
var obj = {}
for (var i = 0; i < fields.length; i++) {
obj[fields[i]] = match[i + 1] || null
}
return obj
}
samples.forEach(function(input) {
console.log(JSON.stringify(parse(input)))
})
Logged output:
{"day1":"MONDAY","rangeType":null,"day2":null,"from":"9AM","to":"10PM","recurrence":"WEEKLY"}
{"day1":"MONDAY","rangeType":"-","day2":"THURSDAY","from":"9AM","to":"10PM","recurrence":"WEEKLY"}
{"day1":"TUESDAY","rangeType":" AND ","day2":"THURSDAY","from":"9AM","to":"10PM","recurrence":"WEEKLY"}
{"day1":"MONDAY","rangeType":null,"day2":null,"from":"9AM","to":"MIDDAY","recurrence":"BI-WEEKLY"}
{"day1":"MONDAY","rangeType":"-","day2":"THURSDAY","from":"11PM","to":"MIDNIGHT","recurrence":"MONTHLY"}
{"day1":"TUESDAY","rangeType":" AND ","day2":"THURSDAY","from":"MIDDAY","to":"3PM","recurrence":"BI-MONTHLY"}
{"day1":"THURSDAY","rangeType":null,"day2":null,"from":"MIDNIGHT","to":"2AM","recurrence":"MONTHLY"}
Looks readable to me. What problem are you trying to solve?
@jasiek it's just to valid some input for Radio Show scheduling. Doesn't need to be anything fancy for now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@miksago not really, but this may help http://regex101.com/r/mL8iA4/1