如何将此日期格式与正则表达式匹配?
嗨,所有正则表达式大师在那里,我知道你有一个解决我的问题。和合如何将此日期格式与正则表达式匹配?
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个字母的版本:例如,一月,二月,三月
版本更新相匹配的更改问题:
[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
对不起,不验证2月和一个月的天数,但也有不值得做的正则表达式中的一些东西;)
使用的SimpleDateFormat代替使用正则表达式。请参阅http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html以获取更多信息。
SIMPLEDATEFORMAT有一个BUG !!,请参阅http://stackoverflow.com/questions/5985912/simpledateformat-bug。 – MarkJ 2011-05-13 01:50:03
/**
* 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);
请注意,字符代表任何字符。
请不要以一种格式提问,等待答案,然后更改问题的格式。如果您需要这样做,请在您的问题文本中明确标记为编辑。 – Town 2011-05-13 01:51:05
我不知道如何标记这个问题作为编辑,嘿嘿:) – MarkJ 2011-05-13 02:05:24