Is regex line end optimized in .NET?

In addition, I know that I should not understand HTML like this with a regex, but this is the easiest for what I need.

I have this regex:

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

Note that I am looking for the end of a line using $ .

Are .NET regular expressions optimized so that the entire string is not scanned? If not, how can I optimize it to start at the end?

+6
source share
1 answer

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

I believe the key point:

By default, the regex engine looks from left to right.

You can change the direction of the search using the RegexOptions.RightToLeft Parameter. The search automatically starts at the last position of the string character. To match the pattern, methods that include a start position parameter, such as Regex.Match (String, Int32), the start position is the index of the extreme position of the character at which the search begins.

Important:

The RegexOptions.RightToLeft parameter changes only the direction of the search; it does not interpret the regex pattern with left

+9
source

Source: https://habr.com/ru/post/897890/


All Articles