在JSON字符串中查找多种日期格式 [英] Find multiple date formats in JSON string

查看:85
本文介绍了在JSON字符串中查找多种日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Java进行工作,并且遇到一个问题,使用正则表达式匹配JSON字符串中的多种日期格式。

I am currently working in Java, and I have an issue matching multiple date formats in a JSON string using a Regex.

JSON:

{"x": "02/23/2019", "y": "02-27-2019"}

正则表达式:

[0-9]{1,2}(/|-)[0-9]{1,2}(/|-)[0-9]{4}

在Regex测试器中,此正则表达式匹配两个日期。但是在Java代码中,我只能从小组中得到一个约会。第二组只是一个 \。

In a Regex tester, this regex matches both dates. But in the Java code, I only get one date from the group. The second group is just a "\".

Java代码:

private static void findDates() {
    String regex = "[0-9]{1,2}(/|-)[0-9]{1,2}(/|-)[0-9]{4}";
    Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    String json =
            "{\"x\":\"02/23/2019\",\n" +
             "\"y\":\"02-27-2019\"}";
    Matcher matcher = pattern.matcher(json);
    if (matcher.find()) {
        for (int i = 0; i < matcher.groupCount(); i++) {
            String dateMatch = matcher.group(i);
            System.out.println(dateMatch);
        }
        System.out.println(json);
    }
}

我需要能够捕获所有出现的日期匹配正则表达式指定的格式。因此,如果JSON中碰巧有三个日期为MM / dd / yyyy或MM-dd-yyyy格式,则当我遍历组时,我应该获取所有三个日期,或者所有五个日期,或者所有两个日期,等等。 ..

I need to be able capture all occurrences of dates that match the format specified by the regex. So if there happen to be three dates in the JSON with MM/dd/yyyy or MM-dd-yyyy formats, when I iterate over the groups, I should get all three dates, or all five dates, or all two dates, etc..

推荐答案

您的代码有点不正确。当您尝试查找所有匹配项时,需要使用 while(matcher.find())。您也可以将(/ |-)编写为 [/-] 。看看这个Java代码。

Your code is a bit incorrect. When you are trying to find all the matches, you need to use while(matcher.find()). Also you can write (/|-) as [/-]. Check out this Java code.

String regex = "[0-9]{1,2}([/-])[0-9]{1,2}\\1[0-9]{4}";
Pattern pattern = Pattern.compile(regex);
String json = "{\"x\":\"02/23/2019\",\n" + "\"y\":\"02-27-2019\"}";

Matcher matcher = pattern.matcher(json);
while (matcher.find()) {
    System.out.println(matcher.group());
}

打印两个日期,

02/23/2019
02-27-2019

注意,我写的是 \\1 而不是([/-])在正则表达式中的年份部分之前,因此它与格式为 02-23 / 2019 02 / 23-2019 ,而仅 02-23-2019 02/23/2019

Notice, I have written \\1 instead of ([/-]) before year part in the regex, so that it doesn't match dates of format, 02-23/2019 or 02/23-2019 and instead only 02-23-2019 and 02/23/2019

另外,在您的代码中,如果使用 if(matcher.find()),则匹配器将首先查找并即使其中许多确实存在,也无法在您的字符串中找到其他匹配项。而 matcher.groupCount()只是动态地为您提供匹配的正则表达式中的组数,用于打印所有组捕获的内容,这并不是您的程序意图

Also, in your code, if you use if (matcher.find()) then matcher will just do first find and will not find further matches in your string even though many of them might indeed exist. And matcher.groupCount() just gives you the number of groups in your matched regex dynamically which you are using to print all the group captures which isn't your intention in your program.

这篇关于在JSON字符串中查找多种日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆