<分区>
输入是这样的:
String text = "{\"definitions\":[{\"id\":\"com.sample.evaluationa\",\"name\":\"Evaluationa\",\"vers}{\"id\":\"com.sample.evaluationb\",\"name\":\"Evaluationb\",\"vers}";
还有一些引用使其更加透明
String definitions = "{\"definitions\":[";
String id = "{\"id\":\"";
String name = "\",\"name\":\"";
String rest = "\",\"vers}";
我的正则表达式看起来像这样:
Pattern pattern = Pattern.compile((Pattern.quote(definitions)) +"("+ (Pattern.quote(id)) +"(.+)" +(Pattern.quote(name))+"(.+)"+(Pattern.quote(rest))+")*");
我正在寻找 id 的 (com.sample.evaluation)
Matcher regexMatcher = pattern.matcher(text);
while (regexMatcher.find()) {
title = regexMatcher.group(2);
System.out.println(title);
System.out.println("The pattern is " + pattern.pattern());
}
我的输出看起来像这样:
com.sample.evaluationa","name":"Evaluationa","vers}{"id":"com.sample.evaluationb
The pattern is \Q{"definitions":[\E(\Q{"id":"\E(.+)\Q","name":"\E(.+)\Q","vers}\E)*
但是我想要:
com.sample.evaluationacom.sample.evluationb
在循环改变目标群体之后,还有什么也很有趣但不是很好的方式
title = regexMatcher.group(2);
我得到了(当然还有图案线)
Evaluationb