Regex for Google Calendar API date output
I have a string containing a date which has a lot of format possibilities. My regular expression to match it is getting ridiculous and long. I'm hoping to learn a more concise expression. Here are the various formats. I'd like to get each date as its own match:
Sat Jul 16, 2011
Sat Jul 16, 2011 6pm to 8pm
Sat Jul 16, 2011 6pm to 8:30pm
Sat Jul 16, 2011 6:30pm to 8pm
Sat Jul 16, 2011 6:30pm to 8:30pm
Sat Jul 16, 2011 to Sun Jul 17, 2011
Sat Jul 16, 2011 6pm to Sun Jul 17, 2011 8pm
Sat Jul 16, 2011 6:30pm to Sun Jul 17, 2011 8pm
Sat Jul 16, 2011 6pm to Sun Jul 17, 2011 8:30pm
Sat Jul 16, 2011 6:30pm to Sun Jul 17, 2011 8:30pm
Before I came here to ask, I'd gotten to this point:
([A-Z]{1}[a-z]{2} [A-Z]{1}[a-z]{2} [0-9]{1,2}, [0-4]{4} [0-9]{1,2}[ap]{1}m)( to ([A-Z]{1}[a-z]{2} [A-Z]{1}[a-z]{2} [0-9]{1,2}, [0-4]{4} [0-9]{1,2}[ap]{1}m))?
Which would return the first date, and the second if it exists, but only for this format: Sat Jul 16, 2011 6pm to Sun Jul 17, 2011 8pm
. That got absurd.
Edit:
Someone suggeste开发者_如何学Cd I check for the "to" portion of the date, and run the regex on each side, rather than trying to account for all cases at once. Still looking for a more concise solution to each side of it, but that will help a lot.
Break the date into two halves as suggested, and then use date.js to parse both halves. Then it's Date.parse('Sat Jul 16, 2011 6:30pm')
精彩评论