gpt4 book ai didi

java - Matcher find() 特定的重复模式

转载 作者:行者123 更新时间:2023-11-30 02:23:12 26 4
gpt4 key购买 nike

这是我的数据:

'%%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/

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