Regex-xpath-如何从字符串中提取子字符串?

Regex-xpath-how to extract a substring from string?

我的 soapui 测试套件中有以下 xpath 匹配,

//html/body/div[2]/table/tbody/tr[td/b[text()='NewsV1']]//td[4]/a[1]/@href[1]

结果:

http://shortness.com:101115/localnewsv1/info

我想要如下预期结果(它应该从 http 到第三个 / 的结果)

http://shortness.com:101115/

我尝试了以下结合正则表达式的xpath

tokenize(/html/body/div[2]/table/tbody/tr[td/b[text()='NewsV1']]//td[4]/a[1]/@href[1], ' ' )[matches(., 'http://+w[a-zA-Z0-9.]+d{*}+/')

这给了我一个无效的表达式 result.if 你有想法指出问题或建议。

Screenshot for the issue

对于此标记,

<a href="http://shortness.com:101115/localnewsv1/info"/>

这个 XPath 2.0 表达式(确保你的库支持 XPath 2.0),

concat(join(tokenize(/a/@href, '/' )[position() < 4],'/'),'/')

将return

http://shortness.com:101115/

根据要求。

我想 d{*} 在某些正则表达式方言中可能有意义,但在 XPath 2.0 正则表达式方言中没有意义(并且无效)。