简体   繁体   English

正则表达式使用c#替换文件中所有出现的文本(大写的第一个字符)

[英]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.

相关问题 C#RegEx一个字典来替换字符串的出现 - C# RegEx a Dictionary to replace occurances of a string C#:使用正则表达式替换某些字符,如果它们是字符串的第一个字符 - C#: Replace Certain Characters if they are the First Character of the String Using Regex 检查第一个字符是否为大写正则表达式 - Checking if first character is Uppercase regex 如何在C#中使用Regex.Replace将单词前的所有文本替换为空格 - How to replace all the text before a word with a blank space using Regex.Replace in C# 使用C#中的Regex替换字符串中所有出现的空格,除了第一次出现的空格 - Replace all occurrences of a white space from string except the first occurrence using Regex in C# C#-Regex使用点替换任何字符。 在括号[]中 - C# - Regex replace any character using dot . in bracket [ ] 使用 C# 正则表达式替换字符串中的字符 - Replace a character inside a string using C# Regex 正则表达式替换字符并在.net C#中转换为大写 - Regex to replace char and convert to uppercase in .net C# 如何使用 C# 中的正则表达式匹配从文本文件中删除或替换多行文本 - How to delete or replace multiple line text from the text file using regex matching in C# 如何替换字符串中的字符并保存到c#中的文本文件中 - how to replace a character in a string and save into text file in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM