Matcher find()一个特定的重复模式

时间:2017-09-18 12:36:17

标签: java regex

这是我的数据:

'%%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();
                }
            }

2 个答案:

答案 0 :(得分:1)

如果您在自定义类之后使用不情愿的量词,则解析器应在下一次出现的双%%时匹配,而不是遍历整个字符串:"(%%[^\"\n]+?%%)"

请注意+之后的问号。

还记得:

matcher.appendTail(stringbuffer)

......一旦你完成了!

<强>文档

参见&#34;量词&#34; documentation的各个部分。

答案 1 :(得分:1)

你的意思是这个正则表达式@echo Off echo First line :: Make sure that the Windows version is compitable. :_VerifyWindowsVersion echo Your Windows version of windows is not supported. Exit

%%(.*?)%%

<强>输出

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
}
相关问题