[英]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.