jbkkd
jbkkd

Reputation: 1550

XPath Wildcard for Numbers

I have some HTML I've extraced in the following format:

<table id="post123">
  <div id="postname">Post Name</div>
</table>
<table id="post124">
  <div id="postname">Post Name 2</div>
</table>

Using Xpath, I'd like to get only the elements which have "post" in them, followed by numbers.

I thought of using:

table[contains(@id, "post")]

But this will also return the element with id "postname", which is not what I want. How would you do that? Is there a wildcard in xpath for numbers?

Also note that the HTML might look like this:

<table id="123post">
  <div id="namepost">Post Name</div>
</table>

Upvotes: 2

Views: 806

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52858

Try using matches() instead.

Something like:

matches(@id,'^post\d+$')

Edit (to match 123post or post123):

matches(@id,'^\d*post\d*$')

That would also match id="post". If that's a problem, you could use:

matches(@id,'(^post\d+$|^\d+post$)')

Upvotes: 2

Related Questions