'MarkLogic matches function with regular expression

I am new to XQuery so could you please help me to understand what is § and §.*$ in below MarkLogic XQuery:

if (matches($cite, '§'))
  replace($cite,'§.*$','')

here $cite := "HI CONST Preamble"



Solution 1:[1]

In regex, the $ in a regex expression is an anchor point to the end of the input string value.

§ is a numeric entity reference of the § character, and . is a wildcard and the * is a quantifier meaning zero to many.

The matches() expression is testing whether the $cite contains the § character. If it does, then it attempts to replace() the § and all of the characters following it until the end of the input with nothing.

For example:

let $cite := "HI CONST Preamble"
return
  if (matches($cite, '§')) 
  then replace($cite,'§.*$','') 
  else "no match"

returns: "no match" because it doesn't contain the § at all.

However, this:

let $cite := "HI CONST Preamble §foo bar baz."
return
  if (matches($cite, '§')) 
  then replace($cite,'§.*$','') 
  else "no match"

Returns: "HI CONST Preamble" because it does contain §, so §foo bar baz. is replaced with "".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1