.net 我可以在 XPath 表达式中使用正则表达式吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/405060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:55:19  来源:igfitidea点击:

Can I use a Regex in an XPath expression?

.netxmlregexxpath

提问by ripper234

Something like .//div[@id='foo\d+]to capture div tags with id='foo123'.

类似于.//div[@id='foo\d+]捕获 div 标签的东西id='foo123'

I'm using .NET, if that matters.

我正在使用 .NET,如果这很重要的话。

回答by Dimitre Novatchev

As other answers have noted, XPath 1.0does not support regular expressions.

正如其他答案所指出的,XPath 1.0不支持正则表达式

Nonetheless, you have the following options:

尽管如此,您有以下选择

.//div
   [starts-with(@id, 'foo') 
  and 
   'foo' = translate(@id, '0123456789', '')
  and
   string-length(@id) > 3   
   ]

回答by Cristian Vat

XPath 2.0 has some functions which support regular expressions: matches(), replace(), tokenize().

XPath 2.0 有一些支持正则表达式的函数:matches(), replace(), tokenize().

In XPath 1.0 there is no regex support.

在 XPath 1.0 中没有正则表达式支持。

For .NET you can use the XPath engine in Saxon.Net to have XPath 2.0 support.

对于 .NET,您可以使用 Saxon.Net 中的 XPath 引擎来获得 XPath 2.0 支持。

So, if using the XPath 2.0 engine in Saxon.NET, your example would turn to: .//div[matches(@id,'foo\d+')].

所以,如果使用Saxon.NET中的XPath 2.0引擎,你的榜样将转向:.//div[matches(@id,'foo\d+')]

回答by annakata

In .NET you have the ability to access your custom classes (and therefore regex if you can code it appropriately for your needs) via Extension Objects.

在 .NET 中,您可以通过扩展对象访问您的自定义类(如果您可以根据需要对其进行适当的编码,则可以访问正则表达式)。

Tutorial here.

教程在这里

回答by hoju

I also wanted to do this so created my own basic xpath module.

我也想这样做,所以创建了我自己的基本xpath 模块