C \ # 정규 표현 식 Regex 상용 API

2850 단어
C \ # 정규 표현 식 Regex 상용 API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;


namespace testRegex
{
    class Program
    {
        static void Main(string[] args)
        {
            //          
            string sourceCode = "this is a source string";
            Regex regex = new Regex("is");
            if (regex.IsMatch(sourceCode))
            {
                Console.WriteLine("sourceCode include is");
            }

            if (Regex.IsMatch(sourceCode , "is"))
            {
                Console.WriteLine("Regex Static Method sourceCode include is");
            }

            string ignoreString = "this is  a iGnoRecAsE string ";

            //RegexOptions.IgnoreCase      
            Regex regex2 = new Regex("ignorecase", RegexOptions.IgnoreCase);
            if (regex2.IsMatch(ignoreString))
            {
                Console.WriteLine("ignoreString include ignorecase");
            }
            if (Regex.IsMatch(ignoreString, "iGnoRecAsE", RegexOptions.IgnoreCase)) ;
            {
                Console.WriteLine("Regex Static Method ignoreString include ignorecase");
            }

            //  
            string replaseString = "this Iss a replace string !";
            string changeStr = Regex.Replace(replaseString,"Iss","is");
            Console.WriteLine( replaseString + "  -->  " + changeStr);
            string chageStrIgnoreCase = Regex.Replace(replaseString, "iss", "is", RegexOptions.IgnoreCase);
            Console.WriteLine(replaseString + "  -->  " + chageStrIgnoreCase);

            string replaceString2 = "123abc456ABCD789";
            Regex regex3 = new Regex("[A-Z]{3}", RegexOptions.IgnoreCase);
            string changeStr2 = regex3.Replace(replaceString2, new MatchEvaluator(OutPutMatch));
            Console.WriteLine(replaceString2 + "  -->  " + changeStr2);


        }

        //             
        private static string OutPutMatch(Match match)
        {
            //return "<b>" + match.Value + "</b>";
            //return match.Value.ToUpper();

            return "[" + match.Value + "]";
           
        }
    }
}

콘 솔
sourceCode include is
Regex Static Method sourceCode include is
ignoreString include ignorecase
Regex Static Method ignoreString include ignorecase
this Iss a replace string !  -->  this is a replace string !
this Iss a replace string !  -->  this is a replace string !
123abc456ABCD789  -->  123[abc]456[ABC]D789
       . . .

좋은 웹페이지 즐겨찾기