xpath to select a pattern with any number + "specific string"

huangapple go评论56阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月14日 16:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定