tags:

views:

96

answers:

4

I want to rename all my sql tables, having the first character upper case.

So:

[blahField] [int] NOT NULL,

should be converted to:

[BlahField] [int] NOT NULL,

(it doesn't matter if [int] becomes [Int].

I am just using a c# console application to do this quickly.

+1  A: 

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,
Mark Byers
A: 

I assume it's only the first "[blabla]" that needs to be uppercased...

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;
    }
Stormenet
You may want to use a couple of negated character classes: `@"\[([a-z])[^]]+][^.]"`The first one will allow for a better range of characters in the table name, and the second will let it skip over everything except the last segment of a multi-part identifier (e.g. [dbo].[myTable]). Plus they perform a little better.
Jeremy Seghi
Good to know, thanks :) Won't change the answer tough, since mrblah doesn't seems to care about it ;)
Stormenet
+1  A: 
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.

Ahmad Mageed
+1  A: 

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.

Jason
the content is coming from a file, and the content is not all in order so to speak.
mrblah
Huh? What do you mean?
Jason