我有一个包含以下日期范围格式变体的字符串。我需要使用单个 java 正则表达式模式查找并替换小时精度。日期范围是可变的。你能为我想出一个正则表达式吗?
字符串示例
发布日期:{05/31/16.23:41:24-?}
发布日期:{05/31/16.23:41:24-06/21/16.23:41:24}
预期结果
发布日期:{05/31/16.23:00:00-?}
发布日期:{05/31/16.23:00:00-06/21/16.23:00:00}
最佳答案
描述
此正则表达式将查找看起来像日期/时间戳的子字符串,例如 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)
----------------------------------------------------------------------
关于java - 正则表达式更新日期精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37956464/