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 TypeName | Type | Description |
$string |
xs:string? |
the string to split |
$regex |
xs:string |
the pattern |
return value |
element()* |
XQuery Function Declarationdeclare 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 ())
} ; |
ExamplesXQuery Example | Results |
---|
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 OnSee AlsoHistory |
Recommended Reading:
|