-1

我制作了一个包含数组列表的函数 Datepattern 并添加了日期模式正则表达式 我的代码如下

私有 ArrayList getDatePatterns() { ArrayList alDate = new ArrayList();

    //Thursday, the 7th December, 2006.00
    alDate.add("\\b(?i)(monday|tuesday|wednesday|thursday|friday|saturday|sunday)[ ]*[,][ ]*(the)[ ]*[0-9]+(st|th|rd|nd)?[ ]*(Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[t]?[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]*[,][ ]*[0-9]+\\b");


    //Examples: 24th July to 3rd August, 2007  24 July to 3 August, 2007  
    alDate.add("\\b(?i)(?:0?[1-9]|[12][0-9]|3[01])(st|th|rd|nd)?([ ])?(Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[t]?[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]?(to)([ ])?(0?[1-9]|[12][0-9]|3[01])(st|th|rd|nd)?([ ])?(Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[t]?[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]*[\\,]?([ ])*([0-9]+)?\\b");

  //Examples: 1.4.94-to-25.1.95 
   // alDate.add("\\b(?i)[0-9]+[ ]*[.][ ]*[0-9]+[ ]*[.][ ]*[0-9]+[ ]*[.][ ]*(to)[ ]*[0-9]+[ ]*[.][ ]*[0-9]+[ ]*[.][ ]*[0-9]+\\b");

    //Examples: 9th & 12th August,   9th and 12th August, 2008 9th to 12th August, 2008 9th/12th August, 2008 9th-12th August, 2008
  ////////  alDate.add("\\b(?i)(?:(from)?([ ])?0?[1-9]|[12][0-9]|3[01])[ ]*(st|th|rd|nd)?([ ])?(and|to|\\/|[\\-]|&)([ ])?(0?[1-9]|[12][0-9]|3[01])[ ]*(st|th|rd|nd)?([ ])?(of)?([ ])?(Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[t]?[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]*[\\,]*([ ])?([0-9]+)?\\b");

    //Example : 28th July 28 July 24 July 2007 24-July-2007 24th July, 2007 24 July, 2007 24th day of July, 2007  21st of May, 2008


    alDate.add("\\b(?i)(Letter No[.]?[ |-])?(?:0?[1-9]|[12][0-9]|3[01])([\\-]|[ ])?(st|th|rd|nd)?([ ])?(day[ ]of[ ])?(of[ ])?(Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[t]?[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)([ ]?[\\,]?([\\-]|[ ])?([0-9]+))\\b");

    //Example :  August 24,  07; November 26-30, 2007
    alDate.add("\\b(?i)(?:Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[.]?|Sept[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]?((0?[1-9]|[12][0-9]|3[01])[ ]?[-][ ]?)?(0?[1-9]|[12][0-9]|3[01])([ ])?[\\,]([ ])?([0-9]+)?\\b");

    //June 08 and June 2008; May'08 and June'08
    alDate.add("\\b(?i)(?:Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[.]?|Sept[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]?([\\,\\'])?([ ])?([0-9]+)?[ ]*(and)[ ]*(?:Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[.]?|Sept[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]?([\\,\\'])?([ ])?([0-9]+)?\\b");
    //Example : March, 2009;  March 2009; June-April, 2008
    alDate.add("\\b(?i)((?:Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[.]?|Sept[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]?[-][ ]?)?(?:Jan[.]?|January|Feb[.]?|February|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[.]?|Sept[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)[ ]?([\\,])?([ ])?[0-9]+\\b");              

    //Examples: from dd-mm-yy to    dd-mm-yy  dd/mm/yy ,  dd.mm.yy   etc
  //  alDate.add("\\b(?i)(i[.]e[.]|viz[.]|means)\\s(from[ ])(0?[1-9]|[12][0-9]|3[01])([\\.]|[\\-]|[ \\/])+(?:0?[1-9]|1[0|1|2])([\\.]|[\\-]|[ \\/])+[0-9]+([ ]to[ ])(?:0?[1-9]|[12][0-9]|3[01])([\\.]|[\\-]|[ \\/])+(?:0?[1-9]|1[0|1|2])([\\.]|[\\-]|[ \\/])+[0-9]+\\b");
  //  alDate.add("\\b(?i)(from[ ])(0?[1-9]|[12][0-9]|3[01])([\\.]|[\\-]|[ \\/])+(?:0?[1-9]|1[0|1|2])([\\.]|[\\-]|[ \\/])+[0-9]+([ ]to[ ])(?:0?[1-9]|[12][0-9]|3[01])([\\.]|[\\-]|[ \\/])+(?:0?[1-9]|1[0|1|2])([\\.]|[\\-]|[ \\/])+[0-9]+\\b");

    // 28th Feburary
    alDate.add("\\b(?i)(the)?[ ]*[0-9]+(st|th|rd|nd)?[ ]*(Jan[.]?|January|February|Feb[.]?|Mar[.]?|March|Apr[.]?|April|May|Jun[.]?|June|Jul[.]?|July|Aug[.]?|August|Sep[t]?[.]?|September|Oct[.]?|October|Nov[.]?|November|Dec[.]?|December)");

    //Examples: dd-mm-yy    , dd/mm/yy ,  dd.mm.yy   etc
    alDate.add("\\b(?i)(?:0?[1-9]|[12][0-9]|3[01])([\\.]|[\\-]|[ \\/])+(?:0?[1-9]|1[0|1|2])([\\.]|[\\-]|[ \\/])+[0-9]+\\b");

    //2002-2003
    //alDate.add("\\b([0-9]+[\\-][0-9]+)\\b");
    return alDate;
}

而不是这个,我想要一个满足上述所有条件的正则表达式。在评论中我提到了正则表达式遵循的日期模式。任何人都可以帮助我,我想为日期模式编写一个单一的正则表达式,而不是这个大代码。我想减少代码。感谢你

4

2 回答 2

3

不要使用 RegEx 进行日期解析,使用SimpleDateFormat. :-)

于 2013-06-10T12:42:35.193 回答
0

不幸的是,你的问题对于你想用你的代码实现什么非常模糊。对我来说,听起来您想解析不同格式的日期字符串,并可能将它们转换DateDateFormat.parse(String).

编辑:我几乎完全确定,正则表达式不是您的代码尝试执行的任务的正确工具,但由于某种原因您不想透露。我很确定,您正在尝试重新实施一个解决方案,以解决一个常见且微妙的问题,其中包含大量陷阱,尤其是在国际化方面。通常这些可以通过一小组库函数来解决,这些函数可以保护程序员(你)、这个混乱的未来维护者以及应用程序的(潜在)用户处理上述问题及其后果。

无论如何,将 RE 粘合在一起会使它的可读性/可维护性更差。

于 2013-06-10T12:48:34.213 回答