嗨,所有正則表達式大師在那裏,我知道你有一個解決我的問題。和合如何將此日期格式與正則表達式匹配?
02-May-2011
或
22-May-2011
或
2-May-2011
(DD-MMM-YYYY) 用YYYY不接受任何其他字符不是數字
嗨,所有正則表達式大師在那裏,我知道你有一個解決我的問題。和合如何將此日期格式與正則表達式匹配?
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