Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast multi-string replacement

Tags:

c#

regex

I need to perform multi string replacement. I have a string where several parts need to be changed according to substitution map.

All replacement must be done in one action - it means if "a" should be replaced with "b" and also "b" must be replaced with "c" and input string is "abc", the result will be "bcc" I have a sollution based on building regex and then replacing all matches. I wrote it some time ago, now I'm refactoring the code and not so satisfied with it. Is there better (faster, simplier) sollution?

This is what I have:

public static string Replace(string s, Dictionary<string, string> substitutions)
{
    string pattern = "";
    int i = 0;
    foreach (string ch in substitutions.Keys)
    {
        if (i == 0)
           pattern += "(" + Regex.Escape(ch) + ")";
        else
           pattern += "|(" + Regex.Escape(ch) + ")";
        i++;
     }

     var r = new Regex(pattern);
     var parts = r.Split(s);

     string ret = "";
     foreach (string part in parts)
     {
         if (part.Length == 1 && substitutions.ContainsKey(part[0].ToString()))
         {
             ret += substitutions[part[0].ToString()];
         }
         else
         {
             ret += part;
         }
      }
      return ret;
} 

And test case:

 var test = "test aabbcc";
 var output = Replace(test, new Dictionary<string, string>{{"a","b"},{"b","y"}});
 Assert.That(output=="test bbyycc");
like image 485
Iale Avatar asked Feb 26 '14 13:02

Iale


People also ask

How do I replace multiples in a string?

replace(/cat/gi, "dog"); // now str = "I have a dog, a dog, and a goat." str = str. replace(/dog/gi, "goat"); // now str = "I have a goat, a goat, and a goat." str = str. replace(/goat/gi, "cat"); // now str = "I have a cat, a cat, and a cat."

How do you replace multiple strings in Python?

Replace multiple different substrings There is no method to replace multiple different strings with different ones, but you can apply replace() repeatedly. It just calls replace() in order, so if the first new contains the following old , the first new is also replaced.

How do you replace multiple occurrences of a string in Java?

You can replace all occurrence of a single character, or a substring of a given String in Java using the replaceAll() method of java. lang. String class. This method also allows you to specify the target substring using the regular expression, which means you can use this to remove all white space from String.

Can I replace multiple characters in a string Python?

01) Using replace() method Python offers replace() method to deal with replacing characters (single or multiple) in a string. The replace method returns a new object (string) replacing specified fields (characters) with new values.


1 Answers

You can replace all this with

var r = new Regex(string.Join("|", substitutions.Keys.Select(k => "(" + k + ")")));     
return r.Replace(s, m => substitutions[m.Value]);

The key things are making use of the string.Join method rather than implementing it yourself, and making use of this Regex.Replace overload and delegates to do the replacement.

like image 76
Tim Rogers Avatar answered Nov 01 '22 19:11

Tim Rogers