我有一个文本文件,如下所示
设置aaadb = test
设置aaadbo = dummyvalue1
设置aaacdo = dummyvalue2
依旧......
我有键和值对的列表 如下所示
键值
aaadb MyTesting
aaadbo MyTesting1
aaacdo MyTesting3
依旧......
现在我想输出如下
设置aaadb = MyTesting
设置aaadbo = MyTesting1
设置aaacdo = MyTesting3
有人可以帮助我使用正则表达式来实现这个目标吗?
答案 0 :(得分:0)
工作样本:
static void Main(string[] args)
{
List<string> textFile = new List<string>() { "set aaadb=test", "set aaadbo=dummyvalue1", "set aaacdo=dummyvalue2" };
Dictionary<string, string> keyValues = new Dictionary<string, string>();
keyValues.Add("aaadb", "MyTesting");
keyValues.Add("aaadbo", "MyTesting1");
keyValues.Add("aaacdo", "MyTesting3");
List<string> result = new List<string>();
foreach (var set in textFile)
{
var splits = set.Split('=');
splits[1] = keyValues.Where(k => k.Key == splits[0].Split(' ')[1]).Select(k => k.Value).FirstOrDefault();
result.Add(string.Join("=", splits));
}
Console.ReadLine();
}
答案 1 :(得分:0)
如果您有键值对列表,请说词典
pom.xml
你可以在 Linq 的帮助下轻松输出所需的格式(这里没有正则表达式):
var source = new Dictionary<string, string>() {
{"aaadb", "MyTesting"},
{"aaadbo", "MyTesting1"},
{"aaacdo", "MyTesting3"},
};
结果:
string report = String.Join(Environment.NewLine, source
.Select(pair => $"{pair.Key}={pair.Value}"));
// Or File.WriteAllText(@"C:\MyFile.txt", report); // if you want to save in the file
Console.WriteLine(report);
在你想解析文件后到键/值对时,你可以将正则表达式放在那里:
aaadb=MyTesting
aaadbo=MyTesting1
aaacdo=MyTesting3