2016-06-21 5 views
0

次の日付範囲形式のバリエーションを含む文字列があります。私は単一のJava正規表現パターンを使用して時間精度を見つけて置き換える必要があります。期間は可変です。あなたは私のための正規表現を思い付くことができますか?日付精度を更新する正規表現

文字列例

published_date:{5月31日/ 16.23:41:24?}

published_date:{5月31日/ 16.23:41:24から06/21/16.23: 41:24}

成果

published_date:{5月31日/ 16.23:00:00-}

published_date:{5月31日/ 16.23:00:00から06/21/16.23:00:00}

答えて

2

説明

この正規表現は、05/31/16.23:41:24ような日付/時刻スタンプのように見えるストリングを見出すであろう。それは、日付と時間の部分をキャプチャし、分と秒を00に置き換えることができます。

([0-9]{2}\/[0-9]{2}\/[0-9]{2}\.[0-9]{2}):[0-9]{2}:[0-9]{2} 

で置き換えます$1:00:00

Regular expression visualization

を例

ライブデモ

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

サンプルテキスト

published_date:{05/31/16.23:41:24-?} 

published_date:{05/31/16.23:41:24-06/21/16.23:41:24} 

は交換

published_date:{05/31/16.23:00:00-?} 

published_date:{05/31/16.23:00:00-06/21/16.23:00:00} 

説明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
    \.      '.' 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
0

した後、これを試してみてください。 ":[0-9] +:[0-9] +"です。

パブリッククラス正規表現{

public static void main(String ar[]){ 
    String st = "05/31/16.23:41:24-06/21/16.23:41:24"; 

    st = st.replaceAll(":[0-9]+:[0-9]+", ":00:00"); 
    System.out.println(st); 

    st = "05/31/16.23:41:24-?"; 

    st = st.replaceAll(":[0-9]+:[0-9]+", ":00:00"); 
    System.out.println(st); 

} 

}

関連する問題