gpt4 book ai didi

html - 为什么我的 XPath 与正则表达式无法匹配?

转载 作者:行者123 更新时间:2023-12-05 00:43:37 25 4
gpt4 key购买 nike

我想用 Xidel 来选择 <section>class="body" 标记如果包含格式为 YYYY.M(M).D(D) 的日期查找并提取一个特定的字符串,该字符串有 8 个字符,可以包含字符和数字。

示例输入 HTML:

<section class="body">
Start 2019.1.12

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

thi1te_t

</section>

命令:

xidel -s input.html -e "//*[@class='body' and contains(text(),'(20\d{2}).(\d{1,2}).(\d{1,2})')]"

出于某种原因,我无法使用此正则表达式。在 regex101.com它工作正常。

我想得到 thi1te_t在最终输出中,可能使用正则表达式 ^.{8}$和 grep。

最佳答案

使用 matches(),它与正则表达式匹配,而不是 contains(),它测试文字子字符串包含。

我还建议使用 . 而不是 text() 因为它是您要匹配的真正目标的元素的 string value ,不是真正的 text() 节点子节点。


总而言之,用于选择目标元素的 XPath 将是:

//*[@class='body' and matches(text(),'(20\d{2}).(\d{1,2}).(\d{1,2})')]

I would like to get thi1te_t in the final output, probably with regex ^.{8}$ and grep.

您可以通过标记化与上述 XPath 匹配的元素的字符串值然后选择与您的目标正则表达式匹配的行来返回该子字符串:

tokenize(//*[@class='body' and matches(text(),'(20\d{2}).(\d{1,2}).(\d{1,2})')], 
'\s*\n\s*')[matches(.,'^.{8}$')]

此 XPath 表达式按要求返回 thi1te_t

关于html - 为什么我的 XPath 与正则表达式无法匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59673337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com