Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex.IsMatch vs string.Contains

Tags:

string

.net

regex

Is there any difference in speed/memory usage for these two equivalent expressions:

Regex.IsMatch(Message, "1000") 

Vs

Message.Contains("1000") 

Any situations where one is better than other ?

The context of this question is as follows: I was making some changes to legacy code which contained the Regex expression to find whether a string is contained within another string. Being legacy code I did not make any changes to that and in the code review somebody suggested that Regex.IsMatch should be replaced by string.Contains. So I was wondering whether the change was worth making.

like image 976
Pradeep Avatar asked Jun 03 '10 00:06

Pradeep


People also ask

Which is faster regex or string contains?

With all that said, String. Contains and String. IndexOf is only useful for checking the existence of an exact substring, but Regex is much more powerful and allows you to do so much more.

Which is faster IndexOf or contains?

NET 4.0 - IndexOf no longer uses Ordinal Comparison and so Contains can be faster.

What does regex IsMatch return?

IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options and time-out interval.

Is regex faster than string replace?

String operations will always be faster than regular expression operations.


2 Answers

For simple cases String.Contains will give you better performance but String.Contains will not allow you to do complex pattern matching. Use String.Contains for non-pattern matching scenarios (like the one in your example) and use regular expressions for scenarios in which you need to do more complex pattern matching.

A regular expression has a certain amount of overhead associated with it (expression parsing, compilation, execution, etc.) that a simple method like String.Contains simply does not have which is why String.Contains will outperform a regular expression in examples like yours.

like image 194
Andrew Hare Avatar answered Sep 23 '22 21:09

Andrew Hare


String.Contains is slower when you compare it to a compiled regular expression. Considerably slower, even!

You can test it running this benchmark:

class Program {   public static int FoundString;   public static int FoundRegex;    static void DoLoop(bool show)   {     const string path = "C:\\file.txt";     const int iterations = 1000000;     var content = File.ReadAllText(path);      const string searchString = "this exists in file";     var searchRegex = new Regex("this exists in file");      var containsTimer = Stopwatch.StartNew();     for (var i = 0; i < iterations; i++)     {       if (content.Contains(searchString))       {         FoundString++;       }     }     containsTimer.Stop();      var regexTimer = Stopwatch.StartNew();     for (var i = 0; i < iterations; i++)     {       if (searchRegex.IsMatch(content))       {         FoundRegex++;       }     }     regexTimer.Stop();      if (!show) return;      Console.WriteLine("FoundString: {0}", FoundString);     Console.WriteLine("FoundRegex: {0}", FoundRegex);     Console.WriteLine("containsTimer: {0}", containsTimer.ElapsedMilliseconds);     Console.WriteLine("regexTimer: {0}", regexTimer.ElapsedMilliseconds);      Console.ReadLine();   }    static void Main(string[] args)   {     DoLoop(false);     DoLoop(true);     return;   } } 
like image 20
user279470 Avatar answered Sep 22 '22 21:09

user279470