ProAnswers.org

Can I use a Regex in an XPath expression?

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

I’m using .NET, if that matters.

XPath 1.0 does not support regular expressions. Nonetheless, you have the following options: •Use an XPath 1.0 expression (note the starts-with() and translate() functions) like this:

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




	
		[b]Use [[/b]](http://exslt.org/regexp/functions/test/index.html)




	
		[b]Use[/b] [](http://www.w3.org/TR/xpath-functions/#func-tokenize))