我有一个字符串,它包含以下日期范围格式变体。我需要使用单一的java regex模式来查找和替换小时精度。日期范围是可变的。你能为我想个理由吗?
字符串示例
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}
发布于 2016-06-22 00:16:54
描述
这个正则表达式会找到类似于日期/时间戳的子字符串,比如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

示例
现场演示
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)
----------------------------------------------------------------------发布于 2016-06-22 00:21:26
尝尝这个。":0-9+:0-9+“。
公共类Regex {
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);
}}
https://stackoverflow.com/questions/37956464
复制相似问题