XQuery Regex
In XQuery, regular expressions can be used to perform pattern matching and text manipulation on strings. XQuery provides support for regular expressions through the use of the fn:matches
function and the fn:replace
function. These functions allow you to match and manipulate strings based on a specified regular expression pattern.
fn:matches
function:
Thefn:matches
function is used to check whether a string matches a specified regular expression pattern. It returns a boolean value (true
orfalse
) indicating whether the pattern is found in the string.
Syntax:
fn:matches($input-string, $pattern)
$input-string
: The string to be checked against the regular expression pattern.$pattern
: The regular expression pattern to match against the input string.
Example:
let $text := "Hello, world!"
return fn:matches($text, "Hello")
Output: true
fn:replace
function:
Thefn:replace
function is used to perform text replacement based on a regular expression pattern. It replaces occurrences of the pattern in the input string with a specified replacement string.
Syntax:
fn:replace($input-string, $pattern, $replacement)
$input-string
: The original string to be processed.$pattern
: The regular expression pattern to find and replace.$replacement
: The string to replace occurrences of the pattern.
Example:
let $text := "Hello, world!"
return fn:replace($text, "world", "Universe")
Output: Hello, Universe!
XQuery regular expressions follow the standard regular expression syntax, and they can be as simple or complex as needed. Regular expressions in XQuery can include various patterns, quantifiers, character classes, and more to define sophisticated search patterns.
It’s important to note that the availability and support for regular expressions might differ slightly depending on the specific XQuery implementation or XQuery processor being used. However, in general, most XQuery implementations support regular expressions as part of the XQuery standard function library.