如何在 XPath“包含"中使用正则表达式功能 [英] How to use regex in XPath "contains" function

查看:27
本文介绍了如何在 XPath“包含"中使用正则表达式功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下正则表达式匹配以下文本 sometext12345_text.

I would like to match the following text sometext12345_text using the below regex.

我在我的一项硒测试中使用了它.

I'm using this in one of my selenium tests.

String expr = "//*[contains(@id, 'sometext[0-9]+_text')]";
driver.findElement(By.xpath(expr));

虽然它似乎不起作用.有人可以帮忙吗?

It doesn't seem to work though. Can somebody help?

推荐答案

XPath 1.0 本身不处理正则表达式,您可以尝试类似

XPath 1.0 doesn't handle regex natively, you could try something like

//*[starts-with(@id, 'sometext') and ends-with(@id, '_text')]

(正如 paul t 所指出的,//*[boolean(number(substring-before(substring-after(@id, "sometext"), "_text")))] 可以用于执行与原始正则表达式相同的检查,如果您还需要检查中间数字)

(as pointed out by paul t, //*[boolean(number(substring-before(substring-after(@id, "sometext"), "_text")))] could be used to perform the same check your original regex does, if you need to check for middle digits as well)

在 XPath 2.0 中,尝试

In XPath 2.0, try

//*[matches(@id, 'sometextd+_text')]

这篇关于如何在 XPath“包含"中使用正则表达式功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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