当您了解实现细节并且源代码可用时,最好的判断方法就是简单地查看它。 :)
简短的回答是:不完全是.
.NET 正则表达式实现中实现的优化是博耶-摩尔字符串搜索作为匹配的第一阶段如果可能。看看源代码对于血淋淋的细节。
从代码本身来看:
// The RegexBoyerMoore object precomputes the Boyer-Moore
// tables for fast string scanning. These tables allow
// you to scan for the first occurance of a string within
// a large body of text without examining every character.
// The performance of the heuristic depends on the actual
// string and the text being searched, but usually, the longer
// the string that is being searched for, the fewer characters
// need to be examined.
这需要一个锚定prefix,其搜索方式为这个功能,其评论说:
/*
* This is the one of the only two functions that should be called from outside.
* It takes a RegexTree and computes the set of chars that can start it.
*/
匹配算法包含返回一个no match如果输入字符串短于计算的前缀,则立即结果。
请注意,它也在寻找anchors当然,并针对这些进行优化。
I did not find a minimum length optimization in the code, but I admit I didn't read it thoroughly (gotta do that one day). But I know other regex implementations which do this kind of optimization (PCRE comes to mind). Anyway, the .NET implementation has its own way of optimizing things, you should rely on that.