Functx 模块和以字符结尾的长字符串
Functx module and long strings with end of chars
我遇到了 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" expected, "Something
blah other"
returned.
删除换行符使测试正常进行。我不明白什么? :)
BR
我认为问题出在 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),'.*'),
'')
else ''
} ;
和正则表达式点.
匹配和replace
默认“如果输入字符串不包含与正则表达式匹配的子字符串,函数的结果是与输入字符串。";如果您添加标志 m
参数
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),'.*'),
'', 'm')
else ''
} ;
你得到了正确的匹配、替换和比较。
我遇到了 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" expected, "Something
blah other" returned.
删除换行符使测试正常进行。我不明白什么? :)
BR
我认为问题出在 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),'.*'),
'')
else ''
} ;
和正则表达式点.
匹配和replace
默认“如果输入字符串不包含与正则表达式匹配的子字符串,函数的结果是与输入字符串。";如果您添加标志 m
参数
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),'.*'),
'', 'm')
else ''
} ;
你得到了正确的匹配、替换和比较。