62

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

I'm using .NET, if that matters.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
ripper234
  • 222,824
  • 274
  • 634
  • 905

4 Answers4

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   
   ]
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • @DimitreNovatchev I will not disturb you always,but sometimes I will. :) I am reading - http://www.w3.org/TR/xpath/#function-last and http://msdn.microsoft.com/en-us/library/ms256233.aspx . – Arup Rakshit Jul 06 '13 at 17:42
  • @Priti, See this for resources: http://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589 – Dimitre Novatchev Jul 06 '13 at 18:26
  • @Priti, View all the answers to the question "Any good XSLT tutorial/book/blog/site online? [closed]" here: https://www.dropbox.com/sh/ovm14omsazrljou/o871HjX0gl – Dimitre Novatchev Jul 06 '13 at 19:36
  • @DimitreNovatchev I started to read - *XPath: Navigating XML with XPath 1.0 and 2.0 Kick Start,By Steven Holzner*. – Arup Rakshit Jul 07 '13 at 07:47
  • @Priti, Good to hear. When you read the book you will have deeper understanding of XPath. – Dimitre Novatchev Jul 07 '13 at 15:45
  • @DimitreNovatchev In between If I need help,then I will email you.Could you share your email please, Sir ? – Arup Rakshit Jul 07 '13 at 15:48
  • @Priti, Finding my email address is not as challenging as an IQ test question. But I can't guarantee that I would ever have the time to process any such request. In case you ask an interesting question on SO, you could ping me, and in case I also find this question interesting, then I might post an answer. This last approach is much more effective because the chances are high *someone* would answer. – Dimitre Novatchev Jul 07 '13 at 17:32
  • @DimitreNovatchev Thanks for your time. :) I am on Ubuntu13.04. So any XPATH testing tool to practice the expressions would you suggest? BTW I searched your blog,but my bad,didn't find your email id.. :( – Arup Rakshit Jul 07 '13 at 17:36
  • @Priti, If you can use IE, then I recommend the tool I developed in 2000 -- the XPath Visualizer. It can be downloaded here: http://huttar.net/dimitre/XPV/TopXML-XPV.html There is also a version for FF, but it stopped working when a few months ago FireFox introduced a new version -- sorry I don't have any time to invest in fixing this. – Dimitre Novatchev Jul 07 '13 at 17:58
  • @DimitreNovatchev I know how much expert you are.. That's why I tried to get in touch with you. OK but I will take the second approach as you said to get help from you. But I have fears of getting down-votes in SO,on very basic questions are thus at risk. So I thought I would unstuck myself by you for those. As you are expert in this domain,so you can understand my pain very quick rather than others. – Arup Rakshit Jul 07 '13 at 18:07
  • 1
    @priti, I do understand, and thank you for your high appreciation. As much as I would like to make myself available as a helper, if I do so for you, then I'd be obliged to do the same for everyone else... And believe me, If I could split myself into a thousand Dimitres, I would be glad to do so and serve everyone. Sadly, we are living in the 21st century. – Dimitre Novatchev Jul 07 '13 at 19:33
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+')].

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Cristian Vat
  • 1,602
  • 17
  • 18
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.

annakata
  • 74,572
  • 17
  • 113
  • 180
  • 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. – the Tin Man Jun 30 '16 at 17:41
0

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
hoju
  • 28,392
  • 37
  • 134
  • 178
  • 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. – the Tin Man Jun 30 '16 at 17:42