开发者

Are end of string regex's optimized in .NET?

Aside: Ok, I know I shouldn't be picking apart HTML like this with a regex, but its the simplest for what I need.

I have this regex:

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

Notice how I'm looking for the end of the string with $.

Are .NET's regu开发者_StackOverflow中文版lar 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:

I believe key point is:

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.

Important:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜