大家好,我知道你们这些正则表达式高手一定有办法解决我的问题。呵呵。
02-May-2011
或者
22-May-2011
或者
2-May-2011
是一个日期格式,其中yyyy只接受数字字符,不接受其他字符。
[0-9]{1,2}/[a-zA-Z]{3}/[0-9]{4}
假设月份是3个字母的版本:例如,Jan,Feb,Mar。
更新的版本以匹配问题的更改:
[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}
正如之前提到的,这并不会验证日期的有效性,它只是验证字符串是否与以下格式匹配: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
很抱歉没有对二月份和每个月的天数进行验证,但是有些事情只是不值得在正则表达式中去做;)
使用SimpleDateFormat而不是使用正则表达式。阅读http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html上的教程以获取更多信息。
好的,这里是日期模式的几乎所有可能的正则表达式变体:
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);
/**
* 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);
}
}
}
如果你想要找到一个日期匹配,使用SimpleDateFormat而不是基于正则表达式的匹配检查应该是合适的解决方案。希望下面的代码片段能够完成这个任务。
public static boolean dateFormatValidate(String strDate,String dateFormat)
{
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
String strParsed = null;
Date parsed =null;
try
{
parsed = formatter.parse(strDate);
strParsed = formatter.format(parsed);
}
catch (ParseException e)
{
MYLogger.logger.info(e.getMessage());
}
return strDate.equals(strParsed);
}