Can I use a Regex in an XPath expression?

ripper234 picture ripper234 · Jan 1, 2009 · Viewed 72.3k times · Source

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

I'm using .NET, if that matters.

Answer

Dimitre Novatchev picture Dimitre Novatchev · Jan 1, 2009

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

Nonetheless, you have the following options:

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