假设我有一个字符串列表,例如{“ boy”,“ car”,“ ball”}和一个文本“ boy卖掉他的汽车来买球”。
给出另一个字符串列表{“ 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]
答案 0 :(得分:3)
如果您想使用正则表达式,则可以使用以下内容:
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);
那只狗卖掉了他的酒吧以买骨头
答案 1 :(得分:2)
无需使用正则表达式,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);