用正则表达式扣出字符串中指定的多处内容

截取打印出所有<record></record>间的内容

//难点在于record之前还有类似<></>标记

//本例我想要的结果是输出:

<record>123<a>demo</a><name>tom</name></record>

<record>456</record>

最间代码如下:

String str = "a<record>123<a>demo</a><name>tom</name></record>yyyy<record>456</record>78";
        Pattern pattern = Pattern.compile("<RECORD>.*?</RECORD>",
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = (Matcher) pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group(0));
        }

相关推荐