Does HtmlAgilityPack have the ability to use regular expressions in its XPATH selector?

拜拜、爱过 提交于 2019-12-01 23:05:06

问题


I would like to be able to create a collection of nodes where the text starts with a word and then a number. For example, given the following:

<p>FINDTHIS 1</p>
<p>FINDTHIS SOMETEXT</p>
<p>FINDTHIS 2</p>

I would like to be able to create a collection consisting of two paragraph nodes: FINDTHIS 1 and FINDTHIS 2.

One possible approach would be to create an xpath query like //p[starts-with(., 'FINDTHIS ')] and then use a regular expression to determine whether or not the next character is a number. If I wanted to obtain a list of matches that returned the above criteria, I could create a regular expression object and test the text for each member in the collection.

Is there a way to utilize a regular expression directly within the selector using HtmlAgilityPack?


回答1:


No, the HTML Agility Pack does not currently support this. It supports XPath version 1 queries, which does not support regular expressions.

That said, you'll have to do as you recommended and select using the XPath expression up to the point where you want to use a regular expression, and then use the Where extension method to filter out the appropriate nodes based on an RegEx instance.




回答2:


It's not available out-of-the-box, but you can add this functionality easily. It's described here: HtmlAgilityPack: xpath and regex



来源:https://stackoverflow.com/questions/11729243/does-htmlagilitypack-have-the-ability-to-use-regular-expressions-in-its-xpath-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!