将字符串中的给定模式替换为值
本文关键字:模式 替换 字符串 | 更新日期: 2023-09-27 18:08:28
给定一个这样格式的文件
// Colour
$primary-colour: if(@Model.PrimaryColour, @primaryColour, #afd05c);
$secondary-colour: if(@secondaryColour, @secondaryColour, #323f47);
// and so on
我正在尝试替换@Model。任何基于字典的东西都应该像这样
var dictionary = new Dictionary<string, string>
{
{"primaryColour", "blue"},
{"secondaryColour", "red"}
};
但我正在努力寻找一种方法。
我正在考虑这样做:
private static String Replace(String str)
{
var dictionary = new Dictionary<string, string>
{
{"primaryColour", "blue"},
{"secondaryColour", "red"}
};
string variableValue;
string pattern = @"@Model.(?<name>'w)";
dictionary.TryGetValue(FirstCharacterToLower("${name}"), out variableValue);
var replacePattern = String.Format("{0}", variableValue);
return Regex.Replace(str, pattern, replacePattern, RegexOptions.IgnoreCase);
}
private static string FirstCharacterToLower(string str)
{
Console.WriteLine(str);
if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
return str;
return Char.ToLowerInvariant(str[0]) + str.Substring(1);
}
但是我传递给FirstCharacterToLower的只是一个字符串{name},我被困在那里了。我想不出有什么办法。
知道从这里要去哪里吗?
感谢编辑:基于sln评论,我做了这个,它工作
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var input = @"
// Colour
$primary-colour: if(@Model.PrimaryColour, @Model.PrimaryColour, #afd05c);
$secondary-colour: if(@Model.SecondaryColour, @Model.SecondaryColour, #323f47);";
Console.WriteLine(Replace(input));
}
private static String Replace(String str)
{
var dictionary = new Dictionary<string, string>
{
{"primaryColour", "blue"},
{"secondaryColour", "red"}
};
var regex = new Regex(@"@Model'.(?<name>'w+)");
var output = regex.Replace(str, v =>
{
string outVariable;
dictionary.TryGetValue(GetNameOfVariable(v.Groups["name"].Value), out outVariable);
return outVariable;
});
return output;
}
private static string GetNameOfVariable(string str)
{
Console.WriteLine(str);
if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
return str;
return Char.ToLowerInvariant(str[0]) + str.Substring(1);
}
}
正如@sln所说,你必须使用委托。
private static String Replace(String str)
{
var dictionary = new Dictionary<string, string>
{
{"primaryColour", "blue"},
{"secondaryColour", "red"}
};
string pattern = @"@Model'.(?<name>'w+)";
return Regex.Replace(str, pattern, m =>
{
string key = m.Groups["name"].Value;
key = FirstCharacterToLower(key);
string value = null;
if (dictionary.TryGetValue(key, out value))
return value;
else
return m.Value;
});
}
你最好描述一个通用的正则表达式来匹配你所有的
钥匙。然后使用委托替换。
var dictionary = new Dictionary<string, string>
{
{"primarycolour", "blue"},
{"secondarycolour", "red"}
};
string line_original =
@"
// Colour
$primary-colour: if(@Model.PrimaryColour, @primaryColour, #afd05c);
$secondary-colour: if(@secondaryColour, @secondaryColour, #323f47);
// and so on
";
Regex RxColors = new Regex( @"@Model'.(?<name>'w+)" );
string line_new = RxColors.Replace(
line_original,
delegate(Match match)
{
string outVal;
if ( dictionary.TryGetValue( match.Groups["name"].Value.ToLower(), out outVal) )
return outVal;
return match.Groups[0].Value;
}
);
Console.WriteLine("New line: 'r'n'r'n{0}", line_new );
输出:New line:
// Colour
$primary-colour: if(blue, @primaryColour, #afd05c);
$secondary-colour: if(@secondaryColour, @secondaryColour, #323f47);
// and so on