简体   繁体   中英

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 suggested 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM