2017-12-11 4 views
0

間でテキストを抽出するために、どのように我々は</child>後にコンテンツを抽出するために適用することができ、 <child>次の前に、この文字列が抽出されなければならない正規表現このは、例えば、二つの異なるXMLタグ複数行

<parent> 
    <child>SomeText</child>sometext<otherChild>sometext</otherChild> 
    <child>SomeText2</child>somtext2<otherChild>sometext2</otherChild> 
</parent> 

のようないくつかのXMLを持っていますグループ1のsometext<otherChild>sometext</otherChild>には、somtext2<otherChild>sometext2</otherChild>が含まれている必要があります。

は既にこのような正規表現を適用しようとしましたが、それは最初のマッチこれは動作するはず

String textToParse = ...; 
Pattern pattern = Pattern.compile("(?<=</child>)(.*?)(?=<child>)", Pattern.DOTALL); 

     final Matcher matcher = pattern.matcher(textToParse); 
     if (matcher.find()) { 
      LOGGER.info(matcher.group()); 
     } 
+1

[idene demo]について(https://www.ideone.com/NcJiwh) –

答えて

1

に対してのみ機能:最後の試合で何次<child>がないため|</parent>を追加

Pattern pattern = Pattern.compile("(?<=</child>)(.*?)(?=<child>|</parent>)", Pattern.DOTALL); 

タグ。

さらに次の試合に行くには、matcher.find()matcher.group()をもう一度実行する必要があります。

関連する問題