Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex.Match() vs new Regex().Match() is there a difference?

Tags:

c#

regex

Specifically to memory here, would Regex.Match() and new Regex().Match() have a difference?

Would not creating a new assertion of the Regex class cost more memory then using Regex.Match?

The reason I am asking is I've always been doing new Regex("pattern").Match("Input") and saw Regex.Match and I'm now questioning if I'm doing it wrong!

like image 719
Ma Dude Avatar asked Nov 14 '25 12:11

Ma Dude


1 Answers

With that exact syntax, for you as the developer, there is no difference. except that new Regex() is not a public constructor, you have to pass a pattern.

So in that case the Static probably better.

Where you would you new Regex() is, to assign it to an instance of a Regex like:

var myregex = new Regex("my pattern");

You can then check things on the instance like timeout MatchTimeout and Options.

like image 91
Gerrie Pretorius Avatar answered Nov 17 '25 07:11

Gerrie Pretorius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!