有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中从字符串中提取日期

我有一个字符串,如下所示:

String str = "skip=anthing&id='234234432234' and (timestamp le 2017-01-17T05:05:55:358Z and timestamp ge 2017-01-17T08:05:55:358Z)&format=json";

我想从字符串中提取日期部分,将其放在单引号中,并将其替换回原始字符串

结果字符串应为

String str = "skip=anything&id='234234432234' and (timestamp le '2017-01-17T05:05:55:358Z' and timestamp ge '2017-01-17T08:05:55:358Z')&format=json"

共 (1) 个答案

  1. # 1 楼答案

    您可以使用以下正则表达式

    String input = "skip=anthing&id='234234432234' and (timestamp le 2017-01-17T05:05:55:358Z and timestamp ge 2017-01-17T08:05:55:358Z)&format=json";
    String output = input.replaceAll("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}:\\d{3}Z)", "'$1'");
    System.out.println(output); // skip=anthing&id='234234432234' and (timestamp le '2017-01-17T05:05:55:358Z' and timestamp ge '2017-01-17T08:05:55:358Z')&format=json