2

I am trying to find an equivalent to (.*) from regexes in XPath. Now this is what I currently have:

I have an HTML a-tag with an href to world/nl44/town/ANYNUMBER where ANYNBUMBER So it could for example be: world/nl44/town/12345 and world/nl44/town/1232 is replaced by a random value (I just need the value of the a tag).

The query would look something like this:

$elements = $xpath->query('//a[@href="/world/nl44/town/ANYNUMBER"]'); With ofcourse ANYNUMBER replaced by the XPath equivalent of (.*)

To sum it up:

<a href="/world/nl44/town/12344">Something</a> <- This is what my a looks like, where 12344 could be any number, and I just need the value between the tags, so with this example I would want it to return "Something".

How would I go about this?

L Ja
  • 1,294
  • 9
  • 20

2 Answers2

3

You can use the starts-with() function:

//a[starts-with(@href, "/world/nl44/town/")]
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
0

XPath 2.0

XPath 2.0 has String Functions that Use Pattern Matching where .* itself will work:

So in XPath 2.0, you can use:

//a[matches(@href, '/world/nl44/town/.*')]

XPath 1.0

XPath 1.0 has no support for regex-based pattern matching, only basic string functions. While .* has no direct equivalence, it's sometimes possible to use starts-with() (see @alecxe's solution in this particular case, +1), contains(), or the other XPath 1.0 String Functions to disregard parts of a string to achieve similar results to matching .*. As another example, the start of a string can be disregarded via a clever combination of substring(), string-length(), and string equality checking, effectively implementing ends-with()-like functionality.

Community
  • 1
  • 1
kjhughes
  • 98,039
  • 18
  • 159
  • 218