大多数对正则表达式的解释通常似乎表明预期的行为是使用表达式的贪婪求值从左到右。然而,在一个 XQuery 实现中,我发现交替表达式是并行计算的(显然是为了性能),这意味着“第一个”匹配项不一定是第一个 从左边 匹配的。 p>
例如,在我测试过的所有其他 XQuery 处理器中,给出以下输入:
fn:analyze-string(
'1-a. x. y. z.',
'^(\d+[-\w]*\.?|.{1,10}\.)\s(.+)$',
'ix')
返回相同的输出:
<analyze-string-result>
<match>
<group nr="1">1-a.</group>
<group nr="2">x. y. z.</group>
</match>
</analyze-string-result>
但是,有一个 XQuery 处理器(在组 1 中)在第一个表达式之前计算第二个表达式,并且它匹配:
<analyze-string-result>
<match>
<group nr="1">1-a. x. y.</group>
<group nr="2">z.</group>
</match>
</analyze-string-result>
在 XQuery 的正则表达式规范中,后一个结果将被视为有效的地方是否有任何解释?
最佳答案
基于 https://www.w3.org/TR/xpath-functions-31/#func-analyze-string说
If several alternatives within the regular expression both match at the same position in the input string, then the match that is chosen is the first alternative that matches. For example, if the input string is
The quick brown fox jumps
and the regular expression isjump|jumps
, then the match that is chosen isjump
.
我认为您发布的第一个结果是正确的,产生第二个结果的另一个处理器有错误。
关于regex - 在 XSLT/XQuery 正则表达式中,对交替表达式的求值是否有要求的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165525/