开发者

Combine multiple Regex.Replace calls

开发者 https://www.devze.com 2023-03-06 03:45 出处:网络
I\'ve got a text processor, which includes hundreds of Regex.Replace calls. Many of them work on the same text. They would for example remove white spaces and unwanted 开发者_高级运维characters, put b

I've got a text processor, which includes hundreds of Regex.Replace calls. Many of them work on the same text. They would for example remove white spaces and unwanted 开发者_高级运维characters, put brackets around numbers, remove black-listed words, etc.

Is there a way to make multiple replacements with different patterns with a single call? I'm wondering about that, because my code currently is quite slow and I guess that would save some cycles.


Yes, here is a simple example:

myText = new Regex("hello").Replace(myText, "");
myText = new Regex("goodBye").Replace(myText, "");

Could be replaced with:

myText = new Regex("hello|goodbye").Replace(myText, "");

This may or may not improve your app's performance. It really depends.


Incase, anyone is looking to replace multiple strings with multiple values using Regex. The code

"this is sentence.".Replace("is", "are");
//output-   thare are sentence.

.... sucks because it replace every matching characters. It would not distinguish between "this" and "is". You can use dictionary and Regex like this:

Dictionary<string, string> replacements = new Dictionary<string, string>();
replacements.Add("is", "are");
replacements.Add("this", "these");
string temp;
foreach (KeyValuePair<string,string> replacement in replacements)
{
    address = Regex.Replace(address, @"\b" + replacement.Key + "\\b", replacement.Value);
}

Note: Be careful with the @"\b" + replacement.Key + "\\b" part. It gave me lots of headache.


If it is just for whitespaces, unwanted characters and blacklisted words, why don't you try string / StringBuilder functions?

string newString = oldString.Replace('`','\0');
string newString = oldString.Replace("blackword","");

Also have a look here: Replace multiple words in string and Replace Multiple String Elements in C#

0

精彩评论

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