java—如何在方括号中拆分字符串,如下所示

uqdfh47h  于 2021-07-07  发布在  Java
关注(0)|答案(2)|浏览(524)

我想把绳子分开

[{"starDate":"","endDate":"","relativeDays":,"cronExpression":""},{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}]

{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}
{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}
z9smfwbn

z9smfwbn1#

也许这一行适合您,因为不清楚您是想要两个字符串还是一个带有显式换行符的字符串

String str = "[{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"},{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"}]";

str = String.join("},", str.replaceAll("^\\[|]$+", "").split("},"));
0tdrvxhp

0tdrvxhp2#

理想情况下,您应该以正确的方式使用json解析(如果适用的话)。
同时,对于这种非常特殊的情况,可以使用正则表达式来提取所需的部分(java中的示例):

String input = "[{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"},{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"}]";
Matcher matcher = Pattern.compile("(\\{[^\\}]*\\})").matcher(input);
while( matcher.find() )
    System.out.println(matcher.group(1));

这将导致:

{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}
{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}

相关问题