정규 표현 식 에 사용 되 는 Regex. Matches 정적 방법의 몇 가지 용법

10373 단어 matches
            //①      = >      

            string Text = @"This is a book , this is my book , Is not IIS";



            //         ,       ,         

            string Pattern = "is";



            MatchCollection Matches = Regex.Matches(

                Text,

                Pattern,

                RegexOptions.IgnoreCase |         //     

                RegexOptions.ExplicitCapture |    //      

                RegexOptions.RightToLeft          //         

                );



            Console.WriteLine("");



            foreach (Match NextMatch in Matches)

            {               

                Console.Write("     :{0,2} ", NextMatch.Index);

                Console.Write("     :{0,2} ", NextMatch.Value);

                Console.Write("/n");   

            }



            Console.WriteLine();



            //②     I  

            //“/b”     ,       (      ,       )

            Pattern = @"/bI";

            Matches = Regex.Matches(

                Text,

                Pattern,

                RegexOptions.ExplicitCapture    //      

                );



            Console.WriteLine("");



            foreach (Match NextMatch in Matches)

            {

                Console.Write("     :{0} ", NextMatch.Index);

                Console.Write("     :{0} ", NextMatch.Value);

                Console.Write("/n");

            }



            Console.WriteLine();



            //③     I  ,  S      

            //“/b”     ,       (      ,       )

            ///S*           

            Pattern = @"/bI/S*S/b";

            Matches = Regex.Matches(

                Text,

                Pattern,

                RegexOptions.ExplicitCapture    //      

                );



            Console.WriteLine("");



            foreach (Match NextMatch in Matches)

            {

                Console.Write("     :{0} ", NextMatch.Index);

                Console.Write("     :{0} ", NextMatch.Value);

                Console.Write("/n");

            }



            Console.WriteLine();



            //④  his   iis,       

            Pattern = @"[h|i]is";

            Matches = Regex.Matches(

                Text,

                Pattern,

                RegexOptions.IgnoreCase |         //     

                RegexOptions.ExplicitCapture    //      

                );



            Console.WriteLine("");



            foreach (Match NextMatch in Matches)

            {

                Console.Write("     :{0} ", NextMatch.Index);

                Console.Write("     :{0} ", NextMatch.Value);

                Console.Write("/n");            

            }



            Console.WriteLine();



            //⑤ Url     

            Text = "http://192.168.0.1:2008";

            Pattern = @"/b(/S+)://(/S+)(?::(/S+))/b";

            Matches = Regex.Matches(Text, Pattern);



            Console.WriteLine("");



            foreach (Match NextMatch in Matches)

            {

                Console.Write("     :{0} ", NextMatch.Index);

                Console.Write("     :{0} ", NextMatch.Value);

                Console.Write("/n");



                for (int i = 0; i < NextMatch.Groups.Count; i++)

                {

                    Console.Write("     {0}:{1,4} ", i + 1, NextMatch.Groups[i].Value);

                    Console.Write("/n");

                }

            }



            Console.Read();

좋은 웹페이지 즐겨찾기