简体   繁体   English

当我从条件组中省略“else”时,为什么.NET的正则表达式引擎表现得如此奇怪?

[英]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.

相关问题 为什么.NET抛出StackOverflowException时表现如此糟糕? - Why does .NET behave so poorly when StackOverflowException is thrown? 为什么这个派生的 class 的行为与其基础 class 不同 - Why does this derived class behave different from it's base class 为什么使用后向查看时,被动(非捕捉)组的性能要比普通组好? - Why does a passive (non-capturing) group behave better than a normal one when using backward viewing? 为什么null条件运算符对==和.Equals()的行为不同? - Why does the null-conditional operator behave differently for == and .Equals()? 为什么在将VB.NET代码迁移到C#时,for循环的行为会有所不同? - Why does a for loop behave differently when migrating VB.NET code to C#? 当端点分发程序的消息检查器中发生未处理的异常时,为什么WCF IErrorHandler表现怪异? - Why does a WCF IErrorHandler behave weird when an unhandled exception occurs in a endpoint dispacher's message inspector? 为什么在应该表现为try / catch / finally的情况下使用using会引发异常? - Why does using throw an exception when it's supposed to behave like try/catch/finally? 为什么按匿名对象的键分组不符合预期? - Why group by key of anonymous objects does not behave the way expected? .Net是否使用其他正则表达式引擎? - Does .Net Use Different Regex Engine? 为什么绑定设置在.NET 4与.NET 3.5中的行为不同 - Why does binding setup behave differently in .NET 4 vs .NET 3.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM