62

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

I'm using .NET, if that matters.

0

4 Answers 4

95

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   
   ]
11
28

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

In XPath 1.0 there is no regex support.

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

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

2

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.

Tutorial here.

1
  • The link is broken. When linking to an off-site page please summarize the important information so that, when the link rots and breaks, the gist of the linked page is still available. Jun 30, 2016 at 17:41
0

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

1
  • 1
    The link was broken so I updated it. When linking to an off-site page please summarize the important information so that, when the link rots and breaks, the gist of the linked page is still available. Link-only answers do no good when the link is broken. Jun 30, 2016 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.