为什么我的 XPath 与正则表达式无法匹配?
Why is my XPath with regex failing to match?
我想使用 Xidel 来 select 带有 class="body"
的 <section>
标签(如果包含格式为 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()
,因为它是您真正要匹配的元素的 字符串值 ,而不是真正的目标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.
您可以 return 通过标记化上述 XPath 匹配的元素的字符串值然后选择与您的目标正则表达式匹配的行来 return 该子字符串:
tokenize(//*[@class='body' and matches(text(),'(20\d{2}).(\d{1,2}).(\d{1,2})')],
'\s*\n\s*')[matches(.,'^.{8}$')]
此 XPath 表达式 returns thi1te_t
,按要求。
我想使用 Xidel 来 select 带有 class="body"
的 <section>
标签(如果包含格式为 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()
,因为它是您真正要匹配的元素的 字符串值 ,而不是真正的目标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.
您可以 return 通过标记化上述 XPath 匹配的元素的字符串值然后选择与您的目标正则表达式匹配的行来 return 该子字符串:
tokenize(//*[@class='body' and matches(text(),'(20\d{2}).(\d{1,2}).(\d{1,2})')],
'\s*\n\s*')[matches(.,'^.{8}$')]
此 XPath 表达式 returns thi1te_t
,按要求。