3

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?

Oded
  • 489,969
  • 99
  • 883
  • 1,009

2 Answers2

2

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.

Community
  • 1
  • 1
casperOne
  • 73,706
  • 19
  • 184
  • 253
1

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

Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298