开发者

Using Regex.Match static method to find a match starting at a specific position

开发者 https://www.devze.com 2023-03-18 17:01 出处:网络
Microsoft offers a method called Match that matches a Regex pattern starting at a specific position in the input string. What I am trying to do is opt开发者_C百科imize the performance of my program by

Microsoft offers a method called Match that matches a Regex pattern starting at a specific position in the input string. What I am trying to do is opt开发者_C百科imize the performance of my program by using the static method version of Match, there by gaining the benefit of caching. There does not seem to be a way to specify a specific position to start matching though, even though the member version does. Is there anyway to emulate this or is there an alternate static method I'm missing that allows me to start searching for my pattern at a specific spot in the input string? Any help would be appreciated.


If you use a disassembler tool like DotPeek to look inside the System.dll, you'll see that the implementation of the static function creates a new Regex object:

public static Match Match(string input, string pattern)
{
  return new Regex(pattern, RegexOptions.None, true).Match(input);
}

public static Match Match(string input, string pattern, RegexOptions options)
{
  return new Regex(pattern, options, true).Match(input);
}

So in fact it's just the opposite - the static function has worse (or at least not better) performance.

0

精彩评论

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

关注公众号