I need one string to be checked and modified many times (searching and replacing different seqences) but it is not working well. I guess its because of the immutability.
private string DoRegexCheck(string line)
{
string pattern;
foreach (string re in this.regexPatterns.Items)
{
pattern = re;
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
line=r.Replace(line, ""); //the line should be updated and the check should continue with updated line
}
return line;
}
“it is not working well” is somewhat ambiguous.
If you mean “it isn’t changing
line– then that code is fine. Immutability is not an issue at all, since we are changing to a new string eachReplace(the code as shown does not try to edit an existing string). If it isn’t updating as expected, then yourRegexpatterns are simply incorrect.If you mean performance: you can’t change the way
Regexworks on strings; I would, however, suggest caching the various regex using theCompiledoption, so that you have an array or dictionary of pre-compiledRegexthat you re-use. This is especially important if applying this for thousands oflines.