首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新数据精度的Regex

更新数据精度的Regex
EN

Stack Overflow用户
提问于 2016-06-21 23:59:35
回答 2查看 34关注 0票数 0

我有一个字符串,它包含以下日期范围格式变体。我需要使用单一的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}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-22 00:16:54

描述

这个正则表达式会找到类似于日期/时间戳的子字符串,比如05/31/16.23:41:24。它将捕获00的日期和小时部分,并允许您替换分钟和秒。

代码语言:javascript
复制
([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

样本文本

代码语言:javascript
复制
published_date:{05/31/16.23:41:24-?}

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

置换后

代码语言:javascript
复制
published_date:{05/31/16.23:00:00-?}

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

解释

代码语言:javascript
复制
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)
----------------------------------------------------------------------
票数 2
EN

Stack Overflow用户

发布于 2016-06-22 00:21:26

尝尝这个。":0-9+:0-9+“。

公共类Regex {

代码语言:javascript
复制
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);

}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37956464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档