Regex.Replace with compiled option in loop

Good morning,

Let's say I have the following code that allows you to remove any spaces from each line in a given list:

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

Since the documentation RegexOptions.Compiledsays that “It gives faster execution, but increases startup time”, I would like to know if this refers to an increase in startup time for the entire program launch or refers to the launch of each Regex.Replacecall inside the loop, thereby making the whole loop slower .

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

Many thanks.

+3
source share
3 answers

It refers to compilation time of a regular expression. But the Compiled option is for regular expressions that are created once and are often used, so it makes sense to do this outside the loop and reuse it.

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

Return to MSDN :

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

IMHO (Regex.<something>), . , MSDN :

. 15 . 15 , . , Regex.CacheSize .

, regex ( ), , .

+1

Regular expressions are not cached. Each time you explicitly create a new instance or call Regex.Replace, a new instance is created. If the flags are turned on RegexOptions.Compiled, it will compile every time.

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

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

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


All Articles