[英]regex to replace all occurances text, uppercase first character in a file using c#
I want to rename all my sql tables, having the first character upper case. 我想重命名我的所有sql表,并以第一个字符大写。
So: 所以:
[blahField] [int] NOT NULL,
should be converted to: 应该转换为:
[BlahField] [int] NOT NULL,
(it doesn't matter if [int] becomes [Int]. ([int]变为[Int]并不重要。
I am just using ac# console application to do this quickly. 我只是使用ac#控制台应用程序来快速执行此操作。
This does what you want: 这就是您想要的:
string s = "[blahField] [int] NOT NULL,";
s = Regex.Replace(s, @"^\W*\w", new MatchEvaluator(match => match.Value.ToUpper()));
Console.WriteLine(s);
Output: 输出:
[BlahField] [int] NOT NULL,
string input = "[blahField] [int] NOT NULL,";
string pattern = @"\[(.+?)]";
string result = Regex.Replace(input, pattern,
m => "[" +
m.Groups[1].Value.Substring(0, 1).ToUpper()
+ m.Groups[1].Value.Substring(1)
+ "]"
);
This will return [Int] but you said that was ok. 这将返回[Int],但您说没关系。
This works: 这有效:
public static string FixIt(string s) {
return s.Substring(0, 1) + Char.ToUpper(s[1]) + s.Substring(2);
}
or even 甚至
return "[" + Char.ToUpper(s[1]) + s.Substring(2);
Regex seems like overkill here. 正则表达式似乎在这里过分杀伤力。
I assume it's only the first "[blabla]" that needs to be uppercased... 我认为这只是第一个需要大写的“ [blabla]” ...
Otherwise, ignore this answer :) 否则,请忽略此答案:)
static void Main(string[] args)
{
Console.WriteLine(DoTheMrBlah("[blahField] [int] NOT NULL,"));
Console.ReadKey();
}
static string DoTheMrBlah(string mrBlahMe)
{
Match m = Regex.Match(mrBlahMe, @"\[([a-z]){1}([a-zA-Z0-9]+)\](.+)");
if (m.Success)
{
char upperme = m.Groups[1].Value.ToUpper()[0];
return string.Format("[{0}{1}]{2}", upperme, m.Groups[2], m.Groups[3]);
}
else return mrBlahMe;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.