嗯,你可以使用正则表达式和匹配评价回调是这样的:一旦你的
private string EvaluateMatchCallback(Match match) {
string templateInsert = match.Groups["value"].Value;
// or whatever
string replacedText = GetReplacementTextBasedOnTemplateValue(templateInsert);
return replacedText;
}
:
regex = new Regex(@"\{(?<value>.*?)\}",
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
string replacedText = regex.Replace(<text>,
new MatchEvaluator(this.EvaluateMatchCallback));
而且你的评价回调会做这样的事情正则表达式匹配值,你可以做一个区分大小写的比较并返回正确的替换值。
编辑我觉得你试图找到一个文本块中的占位符,而不是担心外壳本身,如果你的模式一直有效,那么你可以检查前两个字符占位符本身会告诉你,你需要在替换表达式中使用的外壳:
string foo = "teMPLATE";
if (char.IsLower(foo[0])) {
if (char.IsLower(foo[1])) {
// first lower and second lower
}
else {
// first lower and second upper
}
}
else {
if (char.IsLower(foo[1])) {
// first upper and second lower
}
else {
// first upper and second upper
}
}
我仍然会使用正则表达式匹配替换占位符,不过这只是我。
如果不是所有的字符/下限 - 只是跳过串occurence? – sll 2012-03-23 20:33:20
如果模板设置为“骆驼案例”时尚,只需通过检查前两个字母来确定其余部分......哦,geee ......我只是意识到 - 任何时候它只取决于一对前导字母 – Agzam 2012-03-23 20:35:27
这些只是4个案例吗?或者你有更多的,例如'{tEmPlAte}',等等? – code4life 2012-03-23 20:39:31