2015-02-24 79 views
2

正則表達式master需要!SimpleDateFormat RegEx格式

我有一個來自服務器的變量時間戳,我需要找到每次使用哪種格式。我嘗試過實現一個正則表達式格式,但他們不工作。我對於正則表達式模式相當陌生,但我仍然嘗試將它們用於自己,或者尋找一個特定的例子,但找不到,所以我問你。

來自服務器的格式可以是這樣的:

「2015年2月23日15時27分31秒UTC」

「2015-01-22T19:38:40Z 「

這裏找到格式代碼:

private static String getFormat(String time) { 
    String firstRegEx = "^\\d{4}-\\d{2}-\\d{2}\'T+\'\\d{2}:\\d{2}:\\d{2}\'Z\'$"; 
    String secondRegEx = "^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\w{3}$"; 

    if (time.toLowerCase().matches(firstRegEx)) { 
     return firstRegEx; 
    } else if (time.toLowerCase().matches(secondRegEx)) { 
     return secondRegEx; 
    } 
    return null; 
} 

燦你看看我的正則表達式並告訴我我做錯了什麼?

+2

爲什麼你想用這個正則表達式?你爲什麼不試試用'SimpleDateFormat'解析字符串?例如,使用正則表達式來表達2月29日的體面驗證將非常困難。 – 2015-02-24 07:19:12

+0

@JonSkeet完全同意你 – 2015-02-24 07:21:57

+2

只檢查日期格式與正則表達式不會產生任何問題。但是使用正則表達式的日期驗證必定會造成一個主要問題。 – 2015-02-24 07:25:46

回答

2

首先,你必須刪除單引號約於焦炭TZ和第二打電話toLowercase()至極將canvert TtZz。刪除:

private static String getFormat(String time) { 
    String firstRegEx = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"; 
    String secondRegEx = "^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\w{3}$"; 

    if (time.matches(firstRegEx)) { 
     return firstRegEx; 
    } else if (time.toLowerCase().matches(secondRegEx)) { 
     return secondRegEx; 
    } 
    return null; 
} 
1
^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$ 

你的第一個正則表達式應該是簡單的this.This將匹配2015-01-22T19:38:40Z。看到演示。

https://regex101.com/r/aI4rA5/4

你的第二個正則表達式工作正常。

+0

哇什麼是一個真棒工具! 謝謝! – Bended 2015-02-24 07:39:48

0

如果這些是僅有的可能的格式那麼它是足夠的,以測試是否date.charAt(10)==「T」

1

我相信這是替代的解決方案中的註釋建議...

public static void main(String[] args) { 
    System.out.println(getFormat("2015-02-23 15:27:31 UTC")); 
    System.out.println(getFormat("2015-01-22T19:38:40Z")); 
} 

private static DateFormat getFormat(String time) { 
    DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); 
    DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
    if (isFormat(format1, time)) { 
     return format1; 
    } else if (isFormat(format2, time)) { 
     return format2; 
    } else { 
     return null; 
    } 
} 

private static boolean isFormat(DateFormat format, String candidate) { 
    return format.parse(candidate, new ParsePosition(0)) != null; 
} 

如果您使用正則表達式來決定如何解析後的你可以捆綁成能夠消耗多種格式的單一方法,這...

public static void main(String[] args) { 
    System.out.println(getDate("2015-02-23 15:27:31 UTC")); 
    System.out.println(getDate("2015-01-22T19:38:40Z")); 
} 

private static Date getDate(String time) { 
    DateFormat[] formats = { new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"), 
          new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z") }; 
    Date date = null; 
    for (DateFormat format : formats) { 
     if ((date = format.parse(time, new ParsePosition(0))) != null) { 
      break; 
     } 
    } 
    return date; 
} 
+0

謝謝我已經從Jens得到了答案,但感謝幫助 – Bended 2015-02-24 07:39:00

+0

您不需要使用引發異常的重載 - 您可以調用帶有'ParsePosition'參數的重載 - 不會拋出異常。 – 2015-02-24 08:20:24

+0

@JonSkeet我不知道,很棒的小費謝謝,回覆更新。 – Adam 2015-02-24 22:28:09

相關問題