C# 入門 & 実践 / C sharp

4-22. 正規表現 / RegularExpressions - 4 - 置換 Replace / MatchEvaluator


置換と置換の際 MatchEvaluator でメソッドにデータを渡して変換です!

        static string DecoText(Match m)
        {
            // 大文字に変換して、両側に + をつけた文字を返します。
            return "+" + m.ToString().ToUpper() + "+";
        }

        static string DecoText2(Match m)
        {
            int i = 0;
            string x = "";
            foreach (Group g in m.Groups)
            {
                 if (g.Equals(m.Groups["Lower"]))
                 {
                     x += g.ToString().ToLower();
                 }
                 else if (g.Equals(m.Groups["Upper"]))
                 {
                     x += g.ToString().ToUpper();
                 }
            }
            return x;
        }


        static void Main()
        {

            string str = @"Microsoft .Net Frameword 2.0 
Microsoft .Net Frameword 2.0 日本語 Language Pack 
Visual C# 2005 Express Edition 
Microsoft SQL Server 2005 Express Edition x86";

            // Regex : 正規表現
            // 単語 を調べる
            Regex myRegex = new Regex(@"([A-Z][a-z]+)");

            Console.WriteLine("■□■ 置換 A ■□■ \n -- 単語を test に変換します。");
            string after = myRegex.Replace( str, "test" );
            Console.WriteLine("{0}",after );

            Console.WriteLine("■□■ 置換 B ■□■ \n -- 単語を 4 つまで test に変換します。");
            after = myRegex.Replace(str, "test", 4);
            Console.WriteLine("{0}", after);

            // メソッドを通す!
            Console.WriteLine("■□■ 置換 C ■□■ \n -- 単語を メソッドを通した文字に変換します。");
            after = myRegex.Replace(str, new MatchEvaluator(DecoText));
            Console.WriteLine("{0}", after);

            // グループを利用して変換後も利用します!
            myRegex = new Regex(@"(?<tango>[A-Z][a-z]+)");
            Console.WriteLine("■□■ 置換 D ■□■ \n -- 単語を ++で囲って変換します。");
            after = myRegex.Replace(str, "+${tango}+");
            Console.WriteLine("{0}", after);

            // 単語を先頭文字2文字目以降を分けて MatchEvaluator を利用しメソッドで
            myRegex = new Regex(@"(?<Lower>[A-Z])(?<Upper>[a-z]+)");
            Console.WriteLine("■□■ 置換 E ■□■ \n -- 単語を 先頭を小文字 2 文字目以降を小文字に");
            after = myRegex.Replace(str, new MatchEvaluator(DecoText2));
            Console.WriteLine("{0}", after);
        }

■□■ 置換 A ■□■
-- 単語を test に変換します。
test .test test 2.0
test .test test 2.0 日本語 test test
test C# 2005 test test
test SQL test 2005 test test x86
■□■ 置換 B ■□■
-- 単語を 4 つまで test に変換します。
test .test test 2.0
test .Net Frameword 2.0 日本語 Language Pack
Visual C# 2005 Express Edition
Microsoft SQL Server 2005 Express Edition x86
■□■ 置換 C ■□■
-- 単語を メソッドを通した文字に変換します。
+MICROSOFT+ .+NET+ +FRAMEWORD+ 2.0
+MICROSOFT+ .+NET+ +FRAMEWORD+ 2.0 日本語 +LANGUAGE+ +PACK+
+VISUAL+ C# 2005 +EXPRESS+ +EDITION+
+MICROSOFT+ SQL +SERVER+ 2005 +EXPRESS+ +EDITION+ x86
■□■ 置換 D ■□■
-- 単語を ++で囲って変換します。
+Microsoft+ .+Net+ +Frameword+ 2.0
+Microsoft+ .+Net+ +Frameword+ 2.0 日本語 +Language+ +Pack+
+Visual+ C# 2005 +Express+ +Edition+
+Microsoft+ SQL +Server+ 2005 +Express+ +Edition+ x86
■□■ 置換 E ■□■
-- 単語を 先頭を小文字 2 文字目以降を小文字に
mICROSOFT .nET fRAMEWORD 2.0
mICROSOFT .nET fRAMEWORD 2.0 日本語 lANGUAGE pACK
vISUAL C# 2005 eXPRESS eDITION
mICROSOFT SQL sERVER 2005 eXPRESS eDITION x86

こんな感じですね。




4-21. 正規表現 / RegularExpressions - 3 - Captures « 4. C# 入門 Level 2 » 4-23. 例外処理 - 1 - try catch finally


C# 入門 & 実践 / C sharp