字符串正则表达式的结尾是否在.NET中优化?

前端之家收集整理的这篇文章主要介绍了字符串正则表达式的结尾是否在.NET中优化?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
旁白:好的,我知道我不应该用正则表达式来分离这样的 HTML,但它最简单的我需要的东西.

我有这个正则表达式:

Regex BodyEndTagRegex = new Regex("</body>(.*)$",RegexOptions.Compiled |
    RegexOptions.IgnoreCase | RegexOptions.Multiline);

注意我是如何用$来寻找字符串的结尾.

是否对.NET的正则表达式进行了优化,以便它不必扫描整个字符串?如果没有,我怎样才能优化它到最后开始?

您可以通过指定 Right-to-Left Mode选项来控制它本身,但正则表达式引擎不会自动优化它,直到您通过指定选项自己完成:

我相信关键点是:

By default,the regular expression engine searches from left to right.

You can reverse the search direction by using the
RegexOptions.RightToLeft option. The search automatically begins at
the last character position of the string. For pattern-matching
methods that include a starting position parameter,such as
Regex.Match(String,Int32),the starting position is the index of the
rightmost character position at which the search is to begin.

重要:

The RegexOptions.RightToLeft option changes the search direction only; it does not interpret the regular expression pattern from right to left

猜你在找的正则表达式相关文章