Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XQuery negate regex

Im searching for a way to negate a regular expression in xquery. Using Oracle with the XMLQuery function.

Ill give an example first:

XMLQuery(
    'for $number in ("2a2", "32", "1234", "12", "32a3", "")
    where ora:matches($number,"^[0-9]{4}$")

    return xs:integer($number)'
    passing xml RETURNING CONTENT
    )

this works perfect except one thing. I want to get the exact opposite of entries which do not match the pattern.

I tried

where fn:not(ora:matches($number,"^[0-9]{4}$"))
where ora:matches($number,"^[0-9]{4}$") = false()
where ora:matches($number,"^[0-9]{4}$") = fn:false()

which all give me this

ORA-01722: Ungültige Zahl 01722. 00000 - "invalid number" *Cause: The specified number was invalid. *Action: Specify a valid number.

like image 212
user5334993 Avatar asked Feb 19 '26 09:02

user5334993


1 Answers

Just use this pure XPath 2.0 expression (XPath 2.0 is a proper subset of XQuery):

("2a2", "32", "1234", "12", "32a3", "12345", "")
                      [not(matches(., "^[0-9]{4}$"))]

When this expression is evaluated (in XQuery, XSLT 2+ or any standalone XPath/XQuery interpreter tool), the wanted, correct result is produced:

2a2 32 12 32a3 12345

One can further simplify this:

("2a2", "32", "1234", "12", "32a3", "12345", "")
                      [not(matches(., "^\d{4}$"))]
like image 55
Dimitre Novatchev Avatar answered Feb 20 '26 22:02

Dimitre Novatchev