home contribute faq download

FunctX XQuery Functions

functx:get-matches-and-non-matches

Splits a string into matching and non-matching regions

Google
Webdatypic.com

Description

The functx:get-matches-and-non-matches function splits a string into parts that match and those that do not match a regular expression. Matching parts are enclosed in a match elements, and non-matching parts are enclosed in a non-match element. The function returns a sequence of match and non-match elements corresponding to these parts, in the order they appear.

The regular expression cannot match a zero-length string, or an error is raised. Overlapping matches do not appear separately as matches.

Arguments and Return Type

NameTypeDescription
$string xs:string? the string to split
$regex xs:string the pattern
return value element()*

XQuery Function Declaration

declare namespace functx = "http://www.functx.com";
declare function functx:get-matches-and-non-matches
  ( $string as xs:string? ,
    $regex as xs:string )  as element()* {

   let $iomf := functx:index-of-match-first($string, $regex)
   return
   if (empty($iomf))
   then <non-match>{$string}</non-match>
   else
   if ($iomf > 1)
   then (<non-match>{substring($string,1,$iomf - 1)}</non-match>,
         functx:get-matches-and-non-matches(
            substring($string,$iomf),$regex))
   else
   let $length :=
      string-length($string) -
      string-length(functx:replace-first($string, $regex,''))
   return (<match>{substring($string,1,$length)}</match>,
           if (string-length($string) > $length)
           then functx:get-matches-and-non-matches(
              substring($string,$length + 1),$regex)
           else ())
 } ;

Examples

XQuery ExampleResults
functx:get-matches-and-non-matches(
   'abc123def', '\d+')
<non-match>abc</non-match>
<match>123</match>
<non-match>def</non-match>
functx:get-matches-and-non-matches(
   'abc123def', '\d')
<non-match>abc</non-match>
<match>1</match>
<match>2</match>
<match>3</match>
<non-match>def</non-match>
functx:get-matches-and-non-matches(
   'abc123def', '[a-z]{2}')
<match>ab</match>
<non-match>c123</non-match>
<match>de</match>
<non-match>f</non-match>

Depends On

functx:replace-firstReplaces the first match of a pattern
functx:index-of-match-firstThe first position of a matching substring

See Also

functx:get-matchesReturn the matching regions of a string

History

Published OnLast UpdatedContributor(s)
2006-08-232007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, https://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery