gpt4 book ai didi

java - 如何使用正则表达式从字符串中提取参数和值

转载 作者:行者123 更新时间:2023-12-02 09:43:34 25 4
gpt4 key购买 nike

我需要从字符串 ((created_date{[1976-03-06T23:59:59.999Z TO *]}|1)) 中提取参数和该参数的值。这里的参数是created_date。值为 1976-03-06T23:59:59.999Z TO *,其中 * 表示无限制。我需要提取如下所示的数据,即它应该是一个字符串数组。

created_date
1976-03-06T23:59:59.999Z
*
1

我尝试了一些在线正则表达式工具来找到合适的正则表达式,并且还在反复试验的基础上尝试了一些代码。

String str = "((created_date{[1976-03-06T23:59:59.999Z TO *]}|1))";
String patt = "\\((.*)\\{(.*)\\}\\|(1|0)\\)";
Pattern p = Pattern.compile(patt);
Matcher m = p.matcher(str);
MatchResult result = m.toMatchResult();
System.out.println(result.group(1));

类似的result.group(2)3..取决于result.groupCount()

我需要提取如下所示的数据,即它应该是一个字符串数组。

created_date

1976-03-06T23:59:59.999Z

*

1

最佳答案

您可以使用以下内容:

String str = "((created_date{[1976-03-06T23:59:59.999Z TO *]}|1))";
String patt = "\\(\\(([^{]+)\\{\\[([^ ]+) TO ([^]]+)]}\\|([01])\\)\\)";
Pattern p = Pattern.compile(patt);
Matcher m = p.matcher(str);
if (m.matches()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
System.out.println(m.group(4));
}

尝试一下 here !

请注意,您需要调用 Matcherfind()matches() 或更罕见的 lookingAt() 然后您才能使用它的大多数其他方法,包括您尝试使用的 toMatchResult()

关于java - 如何使用正则表达式从字符串中提取参数和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56852280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com