猿问

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

我需要从字符串中提取一个参数和该参数的值((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));

similaryresult.group(2)和3.. 取决于result.groupCount().


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


created_date


1976-03-06T23:59:59.999Z


*


1


富国沪深
浏览 161回答 1
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));}在这里试试吧!请注意,您需要先调用 aMatcher的方法find(),matches()或者很少调用lookingAt(),然后才能使用它的大部分其他方法,包括toMatchResult()您尝试使用的方法。
随时随地看视频慕课网APP

相关分类

Java
我要回答