Friday, 27 July 2018

xquery return a regex matched string

Useful function to return the string matched by a regex pattern - note this will only work for a single match in the string

 declare function local:get-string-match($string as xs:string?, $regex as xs:string)  as xs:string? {
 if (matches($string,$regex)) then
  let $length := string-length($string) - string-length(replace($string, $regex,''))
  let $start := string-length(tokenize($string, $regex)[1]) + 1
  return substring($string, $start, $length)
 else ()
 } ;


No comments:

Post a comment