开发者

Regex.Replace with compiled option in a cycle

开发者 https://www.devze.com 2023-01-26 11:07 出处:网络
Good morning, Let\'s say I have the following code, which attemps to remove a开发者_运维技巧ny whitespace from every string in a given list:

Good morning,

Let's say I have the following code, which attemps to remove a开发者_运维技巧ny whitespace from every string in a given list:

foreach (String StrTmp in SomeList)
    Regex.Replace(StrTmp, @"\p{Z}", "", RegexOptions.Compiled)

Since the documentation of RegexOptions.Compiled says that "This yields faster execution but increases startup time", I would like to know if this increased startup time refers to the whole program's startup time or if it refers to the startup of every Regex.Replace function call inside the cycle, thus making the whole cycle slower.

By the way... Isn't there any Regex.Remove(.,.) command to remove every ocurrence of a given regular expression? Basically this is the same as above, but could be shorter and more elegant.

Thank you very much.


It refers to the regex compile time. But the Compiled option is designed for regexes that are created once and used often, so it makes most sense to make it once outside the loop and reuse it.

Regex theRegex = new Regex(@"\p{Z}", RegexOptions.Compiled);
foreach (String StrTmp in SomeList)
  string replacementString = theRegex.Replace(StrTmp, "");


Reffering to MSDN :

In the .NET Framework versions 1.0 and 1.1, all compiled regular expressions, whether they were used in instance or static method calls, were cached. Starting with the .NET Framework 2.0, only regular expressions used in static method calls are cached.

IMHO you should even make it private member of your class and create it only once in object lifecycle or use static call (Regex.<something>) so that it is cached. In the second approach you should note that MSDN says this:

When you use static method calls with a large number of regular expressions. By default, the regular expression engine caches the 15 most recently used static regular expressions. If your application uses more than 15 static regular expressions, some regular expressions must be recompiled. To prevent this recompilation, you can increase the Regex.CacheSize property to an appropriate value.

So if you optimize speed use approach with regex instance in object (or even class) and if memory is your concern use static method call.


Regular expressions are not cached. Every time you explicitly create new instance or call Regex.Replace, new instance is created. If flags include RegexOptions.Compiled, it is compiled every time.

Therefore the code you provided will be slow. For optimal performance, if regular expression is used multiple times, it should be created once, then reused.

Regex re = new Regex(@"\p{Z}", RegexOptions.Compiled);
foreach (String StrTmp in SomeList)
    re.Replace(StrTmp, "");
0

精彩评论

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