私は週末のプロジェクトに取り組んでいます。単純で軽量な XML パーサーです。ただの楽しみとして、Regexes についてもっと学ぶためです。属性と要素でデータを取得できましたが、タグを分離するのに苦労しています。これは私が持っているものです:
CharSequence inputStr = "<a>test</a>abc<b1>test2</b1>abc1";
String patternStr = openTag+"(.*?)"+closeTag;
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
StringBuffer buf = new StringBuffer();
boolean found = false;
while ((found = matcher.find())) {
String replaceStr = matcher.group();
matcher.appendReplacement(buf, "found tag (" + replaceStr + ")");
}
matcher.appendTail(buf);
String result = buf.toString();
System.out.println(result);
Output: found tag (<a>test</a>abc<b1>test2</b1>)abc1
グループ全体ではなく、各タグで「見つかったタグ」を終了する必要があります。それを行う方法はありますか?ありがとう。