2016-06-09 56 views
0

我必須被解析,其可以或可以不包含日期字符串(例如用類似java.time.format.DateTimeFormatter(由於Java 8)或java.text.DateFormat如何搜索可解析的日期字符串

實施例:

搜索格式yyMMdd HH-mm-ss 的在字符串的日期This was happend at: 140327 12-08-12, yeah.

日期的格式可能會發生變化,所以無法使用預定義的章恩。

在String中搜索日期的最佳方式是什麼? 是否有可能從DateTimeFormatter獲取正則表達式,例如?

我唯一的想法是算yMd等,爲它創造一個正則表達式,但它似乎很醜陋的我上面的例子......

編輯:

日期解析類現在已被精簡。

但確切的解析器並不那麼重要: 我需要一些日期解析器模式和正則表達式模式來搜索字符串中的日期,而不是解析找到的字符串。

解析器模式使用的確切語法並不那麼重要,它可以使用上述示例中的其他語法。

我不知道編譯時的確切日期格式。用戶將輸入日期格式模式(如yyMMdd HH-mm-ss),並且我必須在字符串中使用此模式搜索日期。 (我可以寫一個自己的函數的日期格式模式轉變成一個正則表達式模式,但......也許這就是媒體鏈接的存在?)

+0

首先,我建議提供描述您稱爲DateTimeFormatter,DateFormat的鏈接。其次,目前還不清楚可能的格式是什麼。沒有描述它,你很難解決你的問題,我們會幫助你。 – YakovL

+1

當你說「可能包含或不包含DateTimeFormatter,DateFormat或類似的日期」時,我會感到困惑。 SimpleDateFormat或DateTimeFormatter或Date Parsers也可用於解析月份,年份或日期。在這種情況下,字符串中的任何數字都可以被讀爲月,年或日期或任何其他時間值。更好的辦法是定義你所需要的一組格式,並根據需要捕獲它們並進行解析。 –

回答

1

說明

構建基於用戶的期望日期時間的時間格式我隨便拿自己的格式的正則表達式字符串並用已知的正則表達式替換各個塊alents。換句話說,如果他們輸入了yy那麼這意味着正則表達式[0-9]{2}yyyy意味着[0-9]{4}等。

現場演示

https://repl.it/C1Rc/19

Java代碼的

class Main { 

    public static void main(String[] args) { 

     System.out.println(convert("yyyyMMdd")); 
     System.out.println(convert("yyyy-MM-dd")); 
     System.out.println(convert("yyyyMMdd HH:mm")); 
     System.out.println(convert("yyyyMMdd HH:mm:ss")); 
     System.out.println(convert("yyyy MMM dd")); 


    } 

    public static String convert(String original) { 

     String Output = original; 
     System.out.println(""); 
     System.out.println(original); 

     // Year 
     Output = Output.replaceAll("yyyy", "[0-9]{4}"); 
     Output = Output.replaceAll("yy", "[0-9]{2}"); 

     // Month 
     Output = Output.replaceAll("MMMM", "(?:January|Feburary|March|April|May|June|July|August|September|October|November|December)"); 
     Output = Output.replaceAll("MMM", "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"); 
     Output = Output.replaceAll("MM", "(?:0[0-9]|1[0-2])"); // 00-12 

     // Day 
     Output = Output.replaceAll("dddd", "(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)"); 
     Output = Output.replaceAll("ddd", "(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)"); 
     Output = Output.replaceAll("dd", "(?:[0-2][0-9]|3[01])"); // 00-31 

     // Hour 
     Output = Output.replaceAll("HH", "(?:[0-1][0-9]|2[0-3])"); // 24 hour format 
     Output = Output.replaceAll("hh", "(?:0[0-9]|1[0-2])"); // 12 hour format 

     // Minutes 
     Output = Output.replaceAll("mm", "[0-5][0-9]");  // 0-59 

     // Seconds 
     Output = Output.replaceAll("ss", "[0-5][0-9]"); // 0-59 


     // Meridian 
     Output = Output.replaceAll("EE", "(?:AM|PM)");  // AM or PM 
     Output = Output.replaceAll("ee", "(?:am|pm)");  // am or pm 

    // System.out.println(Output); 
    return Output; 
    } 

    } 

樣本輸出

yyyyMMdd 
[0-9]{4}(?:0[0-9]|1[0-2])(?:[0-2][0-9]|3[01]) 

yyyy-MM-dd 
[0-9]{4}-(?:0[0-9]|1[0-2])-(?:[0-2][0-9]|3[01]) 

yyyyMMdd HH:mm 
[0-9]{4}(?:0[0-9]|1[0-2])(?:[0-2][0-9]|3[01]) (?:[0-1][0-9]|2[0-3]):[0-5][0-9] 

yyyyMMdd HH:mm:ss 
[0-9]{4}(?:0[0-9]|1[0-2])(?:[0-2][0-9]|3[01]) (?:[0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] 

yyyy MMM dd 
[0-9]{4} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?:[0-2][0-9]|3[01]) 
+2

此代碼完全符合我的要求。標準的Java庫不提供任何類似的? (也許避免這麼多的替換 - 但是正則表達式的創建可能不會發生這麼多次,不會影響性能。) –

+0

每次用戶更改其原始格式並將其存儲在數據庫以及原始格式。如果您對答案感到滿意,請將其標記爲已接受的答案。 –

1

說明

你僅供一個格式,你的日期將是英寸

([0-9]{2})([0-9]{2})([0-9]{2})\s+([0-9]{2})-([0-9]{2})-([0-9]{2}) 

Regular expression visualization

此表達式將:

  • 匹配的日期和時間的格式yyMMdd hh-mm-ss
  • 捕獲各自在自己捕獲組的時間值的
    • 組0獲取整個日期時間
    • 組1得到年
    • 組2得到的月份
    • 組3得到一天
    • 組4獲取小時
    • 組5得到分鐘
    • 組6獲取第二個

現場演示

https://regex101.com/r/oR3dJ0/1

示例文本

示例:搜索這一點在happend格式 「YYMMDD HH-毫米-β」 中的字符串」的日期: 140327 12-08-12,是的。「

樣品匹配

MATCH 1 
0. [95-110] `140327 12-08-12` 
1. [95-97]  `14` 
2. [97-99]  `03` 
3. [99-101] `27` 
4. [102-104] `12` 
5. [105-107] `08` 
6. [108-110] `12` 

說明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    (      group and capture to \2: 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
)      end of \2 
---------------------------------------------------------------------- 
    (      group and capture to \3: 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
)      end of \3 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    (      group and capture to \4: 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
)      end of \4 
---------------------------------------------------------------------- 
    -      '-' 
---------------------------------------------------------------------- 
    (      group and capture to \5: 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
)      end of \5 
---------------------------------------------------------------------- 
    -      '-' 
---------------------------------------------------------------------- 
    (      group and capture to \6: 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
)      end of \6 
---------------------------------------------------------------------- 
+0

很好的解釋,但是......我不知道編譯時確切的日期格式。如果它不存在任何其他的可能性,我會寫我自己的日期格式模式到正則表達式模式轉換器。 –

+0

無論你自己編寫或使用正則表達式,你仍然需要解決一套可接受的格式。 'yyyyMMdd' 20160708可能會因爲2016年8月7日而中斷。添加各種時間格式,您將看到問題複雜性增加。 –

+0

當然,可接受的日期/時間格式的數量受限於所使用的解析器。用戶將得到確切的格式來搜索。如果用戶輸入'yyyyMMdd',生成的搜索此格式日期的正則表達式一定不能找到'「2016年8月7日」。 –