在.NET是字符串的正则表达式的结束优化?字符串、结束、正则表达式、NET

2023-09-04 09:08:00 作者:罕见硬汉子i

旁白:好的,我知道我不应该采摘除了HTML像这样用正则表达式,但其最简单的我需要的东西的

我有这样的正则表达式:

I have this regex:

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

请注意我在寻找的字符串的结尾用 $

现.NET的正前pressions优化,使得它不必扫描整个字符串?如果没有,我怎么能优化它开始在结束了吗?

Are .NET's regular expressions optimized so that it doesn't have to scan the entire string? If not, how can I optimize it to start at the end?

推荐答案

您可以通过指定的从右到左模式的选项,但正则表达式引擎没有的优化的它本身自动,直到你做你自己指定一个选项:

You can control it itself by specifying Right-to-Left Mode option, but regex engine does not optimize it itself automatically until you do it yourself by specifying an option:

我认为关键的一点是:

默认情况下,常规的前pression引擎搜索从左至右。

您可以使用反向搜索方向   RegexOptions.RightToLeft选项。搜索会自动开始在   字符串的最后一个字符的位置。对于模式匹配   包括一个起始位置参数的方法,如   Regex.Match(字符串,Int32)在,起始位置的索引   最右边的字符位置搜索开始处。

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.

重要提示:

该RegexOptions.RightToLeft选项只改变搜索的方向;   它不跨preT从右到正规EX pression模式   左

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