0

我遇到了处理带有行尾字符的字符串的functx模块的问题。以下代码应该可以工作(?)

declare %unit:test function test:substring-before-last() {

  let $title := 'Something
blah other'

  let $expected-title := 'Something
blah'

  return unit:assert-equals(functx:substring-before-last($title, ' other'),
    $expected-title)
};

但是它失败了

“Something blah”预期,“Something blah other”返回。

删除换行符使测试正常工作。我不明白什么?:)

BR

4

1 回答 1

1

我认为问题在于 functx 函数http://www.xqueryfunctions.com/xq/functx_substring-before-last.html的定义或实现:

declare function functx:substring-before-last
  ( $arg as xs:string? ,
    $delim as xs:string )  as xs:string {

   if (matches($arg, functx:escape-for-regex($delim)))
   then replace($arg,
            concat('^(.*)', functx:escape-for-regex($delim),'.*'),
            '$1')
   else ''
 } ;

和正则表达式点.匹配和replace默认“如果输入字符串不包含与正则表达式匹配的子字符串,则函数的结果是与输入字符串相同的单个字符串。”;如果您添加 flagsm参数

declare function functx:substring-before-last
  ( $arg as xs:string? ,
    $delim as xs:string )  as xs:string {

   if (matches($arg, functx:escape-for-regex($delim)))
   then replace($arg,
            concat('^(.*)', functx:escape-for-regex($delim),'.*'),
            '$1', 'm')
   else ''
 } ;

你得到正确的匹配和替换和比较。

于 2020-10-25T22:08:03.020 回答