2

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.