微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/regex/356615.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐