I am finding I need to locate an almost regexp type string using an xpath query but have not been able to see how to manage it. The current query I am using is:
$result = $xpath->query('//ul/li[starts-with(@id, "message-")]');
Which is resulting in some false positives. It should really be looking for message-123 or message-987 etc. Is there a way to tell xpath to use something similar to a \d (digit) in regexps?
Use this XPath 1.0 expression for 'message-' + integer
pattern:
//ul/li[
@id[
starts-with(., 'message-')
and
number(
substring-after(., 'message-')
)
= floor(
substring-after(., 'message-')
)
]
]
Try this out:
$xpath->query('//ul/li[matches(@id,"^message-\d{3}")]')
Edit: my bad, sorry - try this out:
$xpath->query('//ul/li[php:functionString("preg_match", "/^message-\d{3}/", @id)]')