这是我的数据:
'%%case.r_object_id%%' and application_phase = '%%case.status%%' and rp.quality = '%%case.<reno_application_person>.<child>.quality%%
我想获取所有用双百分号 %%
括起来的对象,我们有 3 个。
根据我的实现,matcher.find() 找到第一个 %%
最后一个%%
在字符串中。所以它只是一个循环。我如何调整我的正则表达式或代码以确保我获得 3 个匹配项?
Matcher matcher = Pattern.compile("(%%[^\"\n]+%%)").matcher(results);
while (matcher.find()) {
try {
matcher.appendReplacement(stringbuffer, getReplacementStringValue(matcher.group().replaceAll("%", "")));
} catch (DocumentGeneratorException e1) {
e1.printStackTrace();
}
}
最佳答案
您的意思是这个正则表达式%%(.*?)%%
:
String regex = "%%(.*?)%%";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
// ^------note to get the group one
}
输出
case.r_object_id
case.status
case.<reno_application_person>.<child>.quality
关于java - Matcher find() 特定的重复模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46279449/