字符串正则表达式的结尾是否在.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

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...