Home > Mobile >  MarkLogic matches function with regular expression
MarkLogic matches function with regular expression

Time:05-15

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"

CodePudding user response:

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 "".

  • Related