代码之家  ›  专栏  ›  技术社区  ›  Daniel

如何用另一个列表中的项替换文本中的字符串列表?

  •  0
  • Daniel  · 技术社区  · 6 年前

    假设我有一个字符串列表{“boy”,“car”,“ball”}和一个文本“the boy self his car to buy a ball”。

    给定另一个字符串列表{“dog”,“bar”,“bone”},我的目标是查找文本中第一个列表的所有匹配项,并将它们替换为第二个列表的字符串:

    BEFORE: the [boy] sold his [car] to buy a [ball]
    AFTER:  the [dog] sold his [bar] to buy a [bone]
    

    我的第一个想法是使用regex,但我不知道如何将字符串列表关联到regex中,我不想编写aho corasick。

    怎么做才是正确的?


    另一个例子:

    Text: aaa bbb abab aabb bbaa ubab
    replacing {aa, bb, ab, ub} for {11, 22, 35, &x}
    
    BEFORE: [aa]a [bb]b [ab][ab] [aa][bb] [bb][aa] [ub][ab]
    AFTER:  [11]a [22]b [35][35] [11][22] [22][11] [&x][35]
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   41686d6564    6 年前

    如果您想使用regex,可以使用以下内容:

    var findList = new List<string>() { "boy", "car", "ball" };
    var replaceList = new List<string>() { "dog", "bar", "bone" };
    
    // Create a dictionary from the lists or have a dictionary from the beginning.
    var dictKeywords = findList.Select((s, i) => new { s, i })
                               .ToDictionary(x => x.s, x => replaceList[x.i]);
    
    string input = "the boy sold his car to buy a ball";
    // Construct the regex pattern by joining the dictionary keys with an 'OR' operator.
    string pattern = string.Join("|", dictKeywords.Keys.Select(s => Regex.Escape(s)));
    
    string output =
        Regex.Replace(input, pattern, delegate (Match m)
        {
            string replacement;
            if (dictKeywords.TryGetValue(m.Value, out replacement)) return replacement;
            return m.Value;
        });
    
    Console.WriteLine(output);
    

    输出:

    那条狗卖掉酒吧买骨头

        2
  •  2
  •   codeandcloud    6 年前

    不需要使用regex, string.Replace 就够了

    var input = "the boy sold his car to buy a ball";
    var oldvalues = new List<string>() { "boy", "car", "ball" };
    var newValues = new List<string>() { "dog", "bar", "bone" };
    var output = input;
    for (int i = 0; i < oldvalues.Count; i++)
    {
        output = output.Replace(oldvalues[i], newValues[i]);
    }
    Console.WriteLine(output);