输入是这样的:
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