我无法使用Jsoup Java库解析以下情况的一些文本。
1 :This is <b>My Text</b> some other <b> </b> text as well <b></b><b>non empty tag1</b> other text
。
预期产量: some other <b> </b> text as well <b></b>
2 :This is <b>My Text</b> some other <b> </b> text as well <b></b><b>non empty tag2</b> other text
。
预期产量: some other <b> </b> text as well <b></b>
3 :This is <b>My Text</b> some other <b> </b> text as well <b></b><b>non empty tag2</b> other text <b></b> <b>non empty tag3</b>
。
预期产量: some other <b> </b> text as well <b></b>
在这里,如果您注意到文本“我的文本”是固定的(静态),但是第二个非空(不要将空格视为值)的B标签值可能会有所不同。正则表达式应该能够提取到之后<b>My Text</b>
的第一个非空<b>
标记之间的文本。
我正在使用Jsoup库,但无法实现上述预期输出。请确保该解决方案对于每种情况都应该是通用的,因为在我看来,该解决方案是动态的。
简单的解决方案可能看起来像
<b>
您感兴趣的元素(包含您要查找的文本的元素)<b>
您只需要记住,JsoupNode
用来存储所有元素(包括不属于标签的文本),而Element
类(extends Node
)可能只包含特定的标签。
因此,例如文字
before <b>bold</b> after<i>italic</i>
将表示为
<node>before </node>
<element tag="B">
<node>bold</node>
</element>
<node> after</node>
<element tag="I">
<node>italic</node>
</element>
因此,例如,如果您select("b")
(将找到<element tab="B">
)并调用nextElementSibling()
它,则会将您移至<element tag="I">
。为了获得这一点,<node>after</node>
您将需要使用nextSibling()
不会消除简单文本节点的方法。
Node
类的可能问题是它不提供text()
可以生成当前节点文本内容的方法(这可以让我们测试当前节点/元素是否具有任何文本)。但是,没有什么能阻止我们强制转换提供这种方法的Node
标记Element
。
因此,我们的解决方案可能如下所示:
public static String findFragment(String html, String fixedStart) {
Document doc = Jsoup.parse(html);
Element myBTag = doc
.select("b:matches(^" + Pattern.quote(fixedStart) + "$)")
.first();
StringBuilder sb = new StringBuilder();
boolean foundNonEmpty = false;
Node currentSibling = myBTag.nextSibling();
while (currentSibling != null && !foundNonEmpty) {
if (currentSibling.nodeName().equals("b")) {
Element b = (Element) currentSibling;
if (!b.text().trim().isEmpty())
foundNonEmpty = true;
}
sb.append(currentSibling.toString());
currentSibling = currentSibling.nextSibling();
}
return sb.toString();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句