[英]How to extract text between two different xml tags multiline
例如,我們有一些這樣的xml
<parent>
<child>SomeText</child>sometext<otherChild>sometext</otherChild>
<child>SomeText2</child>somtext2<otherChild>sometext2</otherChild>
</parent>
可以應用哪個正則表達式來提取</child>
和下一個<child>
之前的內容。應在第1組,第2組中的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());
}
這應該工作:
Pattern pattern = Pattern.compile("(?<=</child>)(.*?)(?=<child>|</parent>)", Pattern.DOTALL);
添加|</parent>
因為在最后一個匹配項中沒有下一個<child>
標記。
另外,您應該再次執行matcher.find()
和matcher.group()
才能進入下一場比賽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.