2013-02-06 38 views
1

我想檢查一個字符串是否處於特定模式。檢查特定模式

例如我想檢查一個字符串是否匹配模式:2012-02-20。

I.E:xxxx-xx-xx當x是數字時。

可能嗎?有人說正則表達式。

+4

那你試試這麼遠嗎? – PermGenError

+0

如果你想解析日期,你應該使用'SimpleDateFormat'類來解析日期。 – nhahtdh

+0

可能嗎?是的,這是可以通過圖靈機解決的更簡單的問題之一。 – Bergi

回答

2

使用這個表達式\d{4}-\d{2}-\d{2}

檢查使用:

yourString.matches(regexString); 
+3

這個anserws OP的問題,但並沒有解決他的問題。由於這顯然是他尋找的一個日期,因此你的匹配時間是「2012-20-20」,這顯然不是有效的日期。 – jlordo

+0

你可以找到很好的日期的正則表達式。人們有時甚至會避免檢查例外情況,即使它做了這項工作,這將提供一個無例外的解決方案。 – CsBalazsHungary

2

你可以做到這一點的SimpleDateFormat解析方法:

final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); 

boolean matchesDateFormat(String date) 
{ 
    try 
    { 
     format.parse(date); 
     return true; 
    } 
    catch(ParseException e) 
    { 
     return false; 
    } 
} 

當然,如果以後去解析無論如何,那麼你可以跳過這一點,只是試圖解析它。

+0

這是不夠的。 SimpleDateFormat只會盲目地接受無效的日期,如「2013-02-29」。也不保證整個字符串與指定的格式匹配。 – nhahtdh

2

如果要測試日期字符串是否是有效日期,最好使用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; 
    } 
} 
+0

查看我對其他答案的評論。你的代碼是不夠的。它會高興地解析'2013-02-29'或'2013-02-28JSfksdjfksf'而不會給出任何錯誤。 – nhahtdh

1

您可以檢查該字符串遵循的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方法之前,對照正則表達式進行檢查。

0

您可以檢查下面的代碼:

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()); 
    } 
}