while(matcher.find()){}ループ内でグループを取得しようとしている XML スタイルの文字列があります。私が使用している正規表現は次のとおりです。
<myset setName="(.+?)">(.*?)</myset>
Java で使用するために変換した場合:
Pattern setPattern = Pattern.compile("<myset setName=\"(.+?)\">(.*?)</myset>");
Matcher matcher = setPattern.matcher(targetString);
while(matcher.find()){
Log.i(TAG, "First group: " + $1 + " Second group: " + $2);
}
$1は setName です -- これは常に少なくとも 1 文字である必要があります。
$2開始タグと終了タグの間のすべて (または何もない) です。これは 0 文字以上にすることができます。
文字列に対して a を実行するfind()と:
<myset setName="test"><lots of stuff in this subtag /></myset>
$1割り当てられtest、$2割り当てられて、完全に機能します<lots of stuff in this subtag />
ただし、find()この文字列
に対して a を実行すると、次のようになります。<myset setName="test"><lots of stuff in this subtag /></myset><myset setName="test2"><more stuff in this subtag /></myset>
その後、$1マッチtestと$2マッチ<lots of stuff in this subtag /></myset><myset setName="test2"><more stuff in here />
意図した動作は、最初にmatchとmatchfind()を持つ必要があることです。次に、2番目にはmatchとmatchが必要です。$1test$2<lots of stuff in this subtag />find()$1test2$2<more stuff in this subtag />
私は明らかな何かを見落としていると確信しています。ありがとう!