如何将此日期格式与正则表达式相匹配?

嗨,所有正则表达的主人,我知道你有一个关于我的问题的工作。 呵呵

02-May-2011 

要么

 22-May-2011 

要么

 2-May-2011 

(dd-MMM-yyyy)yyyy不接受任何其他字符而非数字

 [0-9]{1,2}/[a-zA-Z]{3}/[0-9]{4} 

假设月份为3个字母的版本:例如,1月,2月,3月。

更新版本以匹配问题的更改:

 [0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4} 

如前所述,这实际上不会validation日期,它只是validation字符串是否符合以下格式: 1或2个数字,短划线,3个字母,短划线,4个数字

 ^\d{1,2}/[a-zA-Z]+/\d{4}$ 

可能就是你要找的东西。 虽然技术上正确的是:

 /^([12]\d|3[01])/(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|June?|July?|Aug(ust)?|Sep(t(ember)?)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)/\d{4}$/i 

很抱歉没有validation二月和一个月中的天数,但有些事情在正则表达式中不值得做;)

使用SimpleDateFormat而不是使用regexp。 有关详细信息,请阅读http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html上的教程。

  /** * Created with IntelliJ IDEA. * User: S34N * Date: 2013/07/30 * Time: 8:21 AM * To change this template use File | Settings | File Templates. */ //Import the required classes/packages import javax.swing.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class dateInputScan { public static void main(String args[]) { dateInputScan run = new dateInputScan(); run.dateInputScan(); } public void dateInputScan() { //Instantiating variables String lvarStrDateOfTransaction = null; DateFormat formatter = null; Date lvarObjDateOfTransaction = null; //Use one of the following date formats. lvarStrDateOfTransaction = "29/07/2013"; //lvarStrDateOfTransaction = "29-07-2013"; //lvarStrDateOfTransaction = "20130729"; //lvarStrDateOfTransaction = "2013-07-29"; //lvarStrDateOfTransaction = "29/07/2013"; //You can also add your own regex (Regular Expression) if (lvarStrDateOfTransaction.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})")) { formatter = new SimpleDateFormat("dd/MM/yyyy"); } else if (lvarStrDateOfTransaction.matches("([0-9]{2})-([0-9]{2})-([0-9]{4})")) { formatter = new SimpleDateFormat("dd-MM-yyyy"); } else if (lvarStrDateOfTransaction.matches("([0-9]{4})([0-9]{2})([0-9]{2})")) { formatter = new SimpleDateFormat("yyyyMMdd"); } else if (lvarStrDateOfTransaction.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})")) { formatter = new SimpleDateFormat("yyyy-MM-dd"); } else if (lvarStrDateOfTransaction.matches("([0-9]{4})/([0-9]{2})/([0-9]{2})")) { formatter = new SimpleDateFormat("yyyy/MM/dd"); } try { lvarObjDateOfTransaction = formatter.parse(lvarStrDateOfTransaction); JOptionPane.showMessageDialog(null, "Date: " + lvarObjDateOfTransaction); } catch (Exception ex) { //Catch the Exception in case the format is not found. JOptionPane.showMessageDialog(null, ex); } } } 

好的,这里几乎所有可能的日期模式的正则表达式变体:

 private final static Pattern DATE_PATTERN_1 = Pattern.compile ( "(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat) " + "(?:Jan|Feb|Mar|Apr|May|June?|July?|Aug|Sept?|Oct|Nov|Dec) " + "\\d\\d \\d\\d:\\d\\d:\\d\\d \\S+ \\d\\d\\d\\d", Pattern.CASE_INSENSITIVE); private final static Pattern DATE_PATTERN_2 = Pattern.compile ( "\\d{4}.\\d{2}.\\d{2}", Pattern.CASE_INSENSITIVE); private final static Pattern DATE_PATTERN_3 = Pattern.compile ( "\\d{2}.\\d{2}.\\d{4}", Pattern.CASE_INSENSITIVE); private final static Pattern DATE_PATTERN_4 = Pattern.compile ( "([0-9]{4})([0-9]{2})([0-9]{2})", Pattern.CASE_INSENSITIVE); private final static Pattern DATE_PATTERN_5 = Pattern.compile ( "^([12]\\d|3[01]).(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|June?|July?|Aug(ust)?|Sep(t(ember)?)?|Oct(ober)?|Nov(ember)?|Dec(ember)?).\\d{4})$", Pattern.CASE_INSENSITIVE); 

请注意“。” 任何字符的字符代表。