获取文本后跟特定文本 [英] Get text followed by certain text

查看:25
本文介绍了获取文本后跟特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取文本但只在特定文本之前('---------------').例如.HTML 代码示例:

I need to get the text but only before the certain text ('---------------'). E.g. example of HTML code:

...
<p> This is correct text. Everything after it is wrong</p>
<p>---------------------</p>
<p><strong>This is wrong text</strong></p>
<p> This is wrong another text</p>
...

我正在尝试使用下一个 XPath 表达式来解决这个问题:

I'm trying to solve this with the next XPath expression:

/p/text()[normalize-space()][not(ancestor::p[contains(.,'---')])]

但不幸的是,这并没有按预期工作.

But unfortunately this doesn't work as expected.

感谢您提供正确的解决方案.

Would be appreciate for the correct solution.

推荐答案

此 XPath 将选择紧随其后的兄弟包含 ---p 的文本:

This XPath will select the text of a p whose immediately following sibling contains ---:

//p[following-sibling::p[contains(.,'---')]][1]/text()

这篇关于获取文本后跟特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆