英文:
xpath to select a pattern with any number + "specific string"
问题
xpath for such span with text containing a number and a string
:
//span[contains(text(), '10 days')]
I have tried this one - //span[contains(text(), 'days') and matches(text(), '\d+ days')] but its not working
.
英文:
xpath for such span with text containing a number and a string
<span>10 days</span>
I have tried this one - //span[contains(text(), 'days') and matches(text(), '\d+ days')]
but its not working
答案1
得分: 2
XPath 1.0 不支持正则表达式。幸运的是,`^\d+ days$` 对于你来说足够简单,可以使用 XPath 1.0 提供的有限字符串函数编写等效条件:
//span[
substring-after(text()," ") = "days" and
substring-before(text()," ") != "" and
translate(substring-before(text()," "), "0123456789", "") = ""
]
---
##### 例子
输入:
```xml
<div>
<!-- 匹配的例子 -->
<span>10 days</span>
<span>365 days</span>
<span>2 days</span>
<!-- 不匹配的例子 -->
<span> days</span>
<span>100 </span>
<span>X days</span>
<span>10 months</span>
<span> 3 days</span>
<span>4 days </span>
<span>5 days</span>
<span>6days</span>
</div>
结果:
10 days
365 days
2 days
英文:
XPath 1.0 doesn't support regexps. Fortunately, ^\d+ days$
is simple enough for you to write an equivalent condition with the limited set of string functions that XPath 1.0 provides:
//span[
substring-after(text()," ") = "days" and
substring-before(text()," ") != "" and
translate(substring-before(text()," "), "0123456789", "") = ""
]
example
input:
<div>
<!-- matching examples -->
<span>10 days</span>
<span>365 days</span>
<span>2 days</span>
<!-- non-matching examples -->
<span> days</span>
<span>100 </span>
<span>X days</span>
<span>10 months</span>
<span> 3 days</span>
<span>4 days </span>
<span>5 days</span>
<span>6days</span>
</div>
result:
10 days
365 days
2 days
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论