在你告诉我这是很多正则表达式之前 - 我知道。不要求任何人写任何正则表达式!你知道是否有人已经做过那个正则表达式吗?
这将返回所有模式: CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns() 但此列表并非 100% 准确。有一些不解析的模式 (yy/mm/dd) 和一些未列出的解析模式。引用 en-US 通用 DateTime.Parse
我所做的是分解模式并尝试为每个模式编写正则表达式。
(^|\s)(3[01]|[12]\d|0?[1-9])\s+(January|February|March|April|May|June|July|August|September|October|November|December),\s?(19|20)?\d\d(\s+(0?\d|1\d|2[0-4]):[0-6]\d(:[0-6]\d)?(\s+([AP]M|GMT|[+-]\d\d:?\d\d))?)?
//dd MMMM, yyyy dddd, dd MMMM, yyyy
//dd MMMM, yyyy h:mm tt dddd, dd MMMM, yyyy h:mm tt
//dd MMMM, yyyy hh:mm tt dddd, dd MMMM, yyyy h:mm:ss tt
//dd MMMM, yyyy h:mm:ss tt dddd, dd MMMM, yyyy hh:mm tt
//dd MMMM, yyyy hh:mm:ss tt dddd, dd MMMM, yyyy hh:mm:ss tt
//dd MMMM, yyyy H:mm dddd, dd MMMM, yyyy H:mm
//dd MMMM, yyyy HH:mm dddd, dd MMMM, yyyy HH:mm
//dd MMMM, yyyy H:mm:ss dddd, dd MMMM, yyyy H:mm:ss
//dd MMMM, yyyy HH:mm:ss dddd, dd MMMM, yyyy HH:mm:ss
(^|\s)(3[01]|[12]\d|0?[1-9])(/|-)(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(/|-)\d\d(\s+(0?\d|1\d|2[0-4]):[0-6]\d(:[0-6]\d)?(\s+([AP]M|GMT|[+-]\d\d:?\d\d))?)?
//dd-MMM-yy
//dd-MMM-yy h:mm tt
//dd-MMM-yy h:mm:ss tt
//dd-MMM-yy hh:mm tt
//dd-MMM-yy hh:mm:ss tt
//dd-MMM-yy H:mm
//dd-MMM-yy HH:mm
//dd-MMM-yy H:mm:ss
//dd-MMM-yy HH:mm:ss
(^|\s)(January|February|March|April|May|June|July|August|September|October|November|December)\s(3[01]|[12]\d|0?[1-9])(,\s?|\s)(19|20)?\d\d(\s+(0?\d|1\d|2[0-4]):[0-6]\d(:[0-6]\d)?(\s+([AP]M|GMT|[+-]\d\d:?\d\d))?)?
//MMMM dd, yyyy dddd, MMMM dd, yyyy
//MMMM dd, yyyy h:mm tt dddd, MMMM dd, yyyy h:mm tt
//MMMM dd, yyyy h:mm:ss tt dddd, MMMM dd, yyyy h:mm:ss tt
//MMMM dd, yyyy hh:mm tt dddd, MMMM dd, yyyy hh:mm tt
//MMMM dd, yyyy hh:mm:ss tt dddd, MMMM dd, yyyy hh:mm:ss tt
//MMMM dd, yyyy H:mm dddd, MMMM dd, yyyy HH:mm
//MMMM dd, yyyy H:mm:ss dddd, MMMM dd, yyyy H:mm:ss
//MMMM dd, yyyy HH:mm dddd, MMMM dd, yyyy HH:mm:ss
//MMMM dd, yyyy HH:mm:ss
(^|\s)(19|20)\d\d(/|-)(1[0-2]|0?\d)(/|-)(3[01]|[12]\d|0?[1-9])(\s+(0?\d|1\d|2[0-4]):[0-6]\d(:[0-6]\d)?(\s+([AP]M|GMT|[+-]\d\d:?\d\d))?)?
/yy/MM/dd yyyy-MM-dd
//yy/MM/dd h:mm tt yyyy-MM-dd h:mm tt
//yy/MM/dd hh:mm tt yyyy-MM-dd hh:mm tt
//yy/MM/dd h:mm:ss tt yyyy-MM-dd h:mm:ss tt
//yy/MM/dd hh:mm:ss tt yyyy-MM-dd hh:mm:ss tt
//yy/MM/dd H:mm yyyy-MM-dd H:mm
//yy/MM/dd HH:mm yyyy-MM-dd HH:mm
//yy/MM/dd H:mm:ss yyyy-MM-dd H:mm:ss
//yy/MM/dd HH:mm:ss yyyy-MM-dd HH:mm:ss
(^|\s)(3[01]|[12]\d|0?[1-9])(/|-|/.)(1[0-2]|0?\d)(/|-|/.)(19|20)?\d\d(\s+(0?\d|1\d|2[0-4]):[0-6]\d(:[0-6]\d)?(\s+([AP]M|GMT|[+-]\d\d:?\d\d))?)?
//fr-FR
//dd.MM.yy dd/MM/yy dd-MM-yy dd/MM/yyyy
//dd.MM.yy H:mm dd/MM/yy H:mm dd-MM-yy H:mm dd/MM/yyyy H:mm
//dd.MM.yy H:mm:ss dd/MM/yy H:mm:ss dd-MM-yy H:mm:ss dd/MM/yyyy H:mm:ss
//dd.MM.yy HH' h 'mm dd/MM/yy HH' h 'mm dd-MM-yy HH' h 'mm dd/MM/yyyy HH' h 'mm
//dd.MM.yy HH.mm dd/MM/yy HH.mm dd-MM-yy HH.mm dd/MM/yyyy HH.mm
//dd.MM.yy HH:mm dd/MM/yy HH:mm dd-MM-yy HH:mm dd/MM/yyyy HH:mm
//dd.MM.yy HH:mm:ss dd/MM/yy HH:mm:ss dd-MM-yy HH:mm:ss dd/MM/yyyy HH:mm:ss