regex - 在 XSLT/XQuery 正则表达式中,对交替表达式的求值是否有要求的顺序?

标签 regex xslt xpath xquery

大多数对正则表达式的解释通常似乎表明预期的行为是使用表达式的贪婪求值从左到右。然而,在一个 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 is jump|jumps, then the match that is chosen is jump.

我认为您发布的第一个结果是正确的,产生第二个结果的另一个处理器有错误。

关于regex - 在 XSLT/XQuery 正则表达式中,对交替表达式的求值是否有要求的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165525/

相关文章:

python - 返回多个匹配组

java - 使用正则表达式 (Java) 从这些字符串中提取子字符串

java - 将元素标签替换为空标签

c# - xslt参数条件检查

jquery - xpath:是否可以排除 not 函数中的最后一个元素

xml - XML 输出需要 Xpath 帮助

java - 如何使用 pattern tokenizer 仅索引在 lucene 中以大写字母开头的单词

ios - 如何检查 RegEx 是否返回 nil?

xslt - xsl 求和同级值

xpath - 使用 Xpath 获取属性名称(不是属性值)