[英]Why does .NET's regex engine behave so bizarrely when I omit the “else” from a conditional group?
Code: 码:
Match match = Regex.Match("abc", "(?(x)bx)");
Console.WriteLine("Success: {0}", match.Success);
Console.WriteLine("Value: \"{0}\"", match.Value);
Console.WriteLine("Index: {0}", match.Index);
Output: 输出:
Success: True
Value: ""
Index: 1
It seems that a conditional group without an "else" expression will instead create a lookahead from the first character of the "if" expression and use that as the "else". 似乎没有“else”表达式的条件组将改为从“if”表达式的第一个字符创建一个前瞻,并将其用作“else”。 In this case it would run as if the regex was
(?(x)bx|(?=b))
在这种情况下,它将像正则表达式一样运行
(?(x)bx|(?=b))
What the **** is going on here? ****在这里发生了什么? Is this intentional?
这是故意的吗? It doesn't seem to be documented.
它似乎没有记录。
Edit: An issue has been created in the corefx repository: https://github.com/dotnet/corefx/issues/26787 编辑:已在corefx存储库中创建了一个问题: https : //github.com/dotnet/corefx/issues/26787
I think it may be a mis-optimization. 我认为这可能是错误的优化。 As Alternation Constructs in Regular Expressions points out:
正则表达式中的交替构造指出:
Because the regular expression engine interprets expression as an anchor (a zero-width assertion), expression must either be a zero-width assertion (for more information, see Anchors ) or a subexpression that is also contained in yes .
因为正则表达式引擎将表达式解释为锚(零宽度断言),所以表达式必须是零宽度断言(有关更多信息,请参阅锚点 )或者也包含在yes中的子表达式。
Your expression value satisfies neither of these constraints. 您的表达式值不满足这些约束。 I suspect some form of optimization where, since the expression isn't zero-width the input is advanced until the yes can potentially be satisfied (since that's the only pattern you've given the regex engine to work with)
我怀疑某种形式的优化,因为表达式不是零宽度,输入会被提前,直到肯定可以满足为止(因为这是你给予正则表达式引擎的唯一模式)
As pointed out in the comments, since your expression isn't also contained in yes , the pattern can never match and so it's unlikely too much concern would be raised about the mis-optimization. 正如评论中所指出的那样,由于你的表达式也没有包含在yes中 ,因此模式永远不会匹配,因此不太可能引起对错误优化的过多关注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.