[假设我有一个字符串列表{“ boy”,“ car”,“ ball”}和文字“那个男孩卖了车买球”。
给出另一个字符串列表{“ dog”,“ bar”,“ bone”},我的目标是查找文本中第一个列表的所有出现并将它们替换为第二个列表的字符串:
BEFORE: the [boy] sold his [car] to buy a [ball]
AFTER: the [dog] sold his [bar] to buy a [bone]
[我的最初想法是使用正则表达式,但我不知道如何将字符串列表关联到正则表达式中,并且我不想编写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]
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);
输出:
狗卖掉了他的狗以买骨头