檢查特定模式
回答
使用這個表達式\d{4}-\d{2}-\d{2}
檢查使用:
yourString.matches(regexString);
這個anserws OP的問題,但並沒有解決他的問題。由於這顯然是他尋找的一個日期,因此你的匹配時間是「2012-20-20」,這顯然不是有效的日期。 – jlordo
你可以找到很好的日期的正則表達式。人們有時甚至會避免檢查例外情況,即使它做了這項工作,這將提供一個無例外的解決方案。 – CsBalazsHungary
你可以做到這一點的SimpleDateFormat解析方法:
final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
boolean matchesDateFormat(String date)
{
try
{
format.parse(date);
return true;
}
catch(ParseException e)
{
return false;
}
}
當然,如果以後去解析無論如何,那麼你可以跳過這一點,只是試圖解析它。
這是不夠的。 SimpleDateFormat只會盲目地接受無效的日期,如「2013-02-29」。也不保證整個字符串與指定的格式匹配。 – nhahtdh
如果要測試日期字符串是否是有效日期,最好使用SimpleDateFormat
來檢查。不要使用正則表達式進行驗證,那麼月份是13?日期是50?閏年?
一些例子:
public boolean isValidDate(String dateString) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
df.parse(dateString);
return true;
} catch (ParseException e) {
return false;
}
}
查看我對其他答案的評論。你的代碼是不夠的。它會高興地解析'2013-02-29'或'2013-02-28JSfksdjfksf'而不會給出任何錯誤。 – nhahtdh
您可以檢查該字符串遵循的4位數字的確切格式,破折號-
,2個數字,破折號-
和2位與@ burning_LEGION的正則表達式。但是,它不檢查字符串是否表示有效的日期。你可以指定9999-99-99
,它會通過驗證。
使用SimpleDateFormat是檢查字符串是有效日期並遵循給定格式的表示法的正確方法。除了formatting a date之外,SimpleDateFormat還可用於解析來自String的Date:parse(String)
,parse(String, ParsePosition)
。
默認情況下,SimpleDateFormat爲寬鬆,這意味着它允許無意義的日期,如2013-025-234
通過。使用setLenient(boolean lenient)
到false
就可以解決這個問題。
但是,另一個問題是它也會忽略任何有效日期後的垃圾數據(例如2012-03-23garbage#$%$#%
)。設置寬鬆並不能解決這個問題。我們需要用parse(String, ParsePosition)
方法檢查最後一個位置。
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
// Make the parsing strict - otherwise, it is worse than regex solution
dateFormatter.setLenient(false);
Date date = null;
ParsePosition pos = new ParsePosition(0);
date = dateFormatter.parse(inputString, pos);
if (date != null && pos.getIndex() == inputString.length()) {
// These 3 points are ensured:
// - The string only contains the date.
// - The date follows the format strictly.
// - And the date is a valid one.
} else {
// Valid date but string contains other garbage
// Or the string has invalid date or garbage
}
SimpleDateFormat
將使2013-1-5
通過,我認爲這是一個合理的寬大處理。如果你不想這樣做,你可以在將字符串插入parse
方法之前,對照正則表達式進行檢查。
您可以檢查下面的代碼:
public void test() {
String REG_EXP = "(\\d{4}-[0,1]?\\d{1}-[0,1,2,3]?\\d{1})"; //yyyy-mm-dd formate this can not check boundary condition something like this... 1981-02-30
String REG_EXP1 = "(\\d{4}-\\d{2}-\\d{2})"; // if u just want xxxx-xx-xx where x is number
String input = "date1 1981-09-06 wrong date 9999-22-22 date2 1981-9-09 date3 1981-11-1 date4";
Pattern pattern = Pattern.compile(REG_EXP);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
- 1. 如何通過PHP檢查特定的URL模式?
- 2. 如何檢查字符串是否處於特定模式?
- 3. 模式檢查特定字符串是否必需
- 4. 如何檢查當前URL是否以特定模式結束
- 5. Jquery:檢查屬性ID是否具有特定模式
- 6. 使用ANT檢查文件存在的特定模式
- 7. 如何檢查字符串是否具有特定模式
- 8. 檢查NSDate是否發生在特定模式
- 9. 檢查特定格式的日期
- 10. 使用模型檢查器檢查一個特定跟蹤
- 11. MYSQL查詢找到特定的模式
- 12. 查找嵌入在特定的模式
- 13. 查找列表中的特定模式
- 14. 檢查自定義字符串模式
- 15. 從特定模式
- 16. preg_replace特定模式
- 17. .match特定模式
- 18. 如何根據特定模式忽略方法的檢查方式
- 19. 使用正則表達式來檢查特定的模式在PHP
- 20. 如何確保特定模式總是遵循特定模式?
- 21. Angularjs - 檢查控制器模式模式
- 22. 檢查特定類的div
- 23. 檢查對特定列
- 24. 檢查特定值的TD
- 25. checkstyle禁用特定檢查
- 26. JavaScript檢查特定散列
- 27. angularjs檢查特定的div
- 28. 檢查特定用戶
- 29. 檢查特定小時
- 30. 泊塢檢查格式中檢索特定網絡的IP
那你試試這麼遠嗎? – PermGenError
如果你想解析日期,你應該使用'SimpleDateFormat'類來解析日期。 – nhahtdh
可能嗎?是的,這是可以通過圖靈機解決的更簡單的問題之一。 – Bergi