<!--打赏 End-->

正则

        static void Main(string[] args)
        {
            Regex reg = new Regex("the");
            string str = "today the ,the";
            Match matchSet;
            matchSet = reg.Match(str);
            if (matchSet.Success)
            {
                Console.WriteLine("  "+matchSet.Index);
            }

            if (Regex.IsMatch(str,"the"))
            {
                Console.WriteLine("success");
            }

            MatchCollection mc;
            mc = reg.Matches(str);
            if (mc.Count>0)
            {
                foreach (Match item in mc)
                {
                    Console.Write(item.Index+" ");
                }
                Console.WriteLine();
            }

            string[] words = new string[] { "bad","boy","baaad","baend"};
            foreach (var item in words)
            {
                if (Regex.IsMatch(item,"ba+")) //ba{2}d ba?d ba*
                {
                    Console.Write(item+" ");
                }
            }
            Console.WriteLine();

            string[] text = new string[] {"part","of","this","<b>string</b>","is","bold" };
            string regExp = "<.{1,2}>";
            MatchCollection amatch;
            foreach (var item in text)
            {
                if (Regex.IsMatch(item,regExp))
                {
                    amatch = Regex.Matches(item,regExp);
                    for (int i = 0; i < amatch.Count; i++)
                    {
                        Console.Write(amatch[i].Value+" ");
                    }
                }
            }
            Console.ReadLine();
        }
View Code

 

        static void Main(string[] args)
        {
           /* string str = "the quick brown fox jumped over the lazy dog one time THE 123 +-";
            Console.WriteLine(str.Length);
            MatchCollection mc;
            mc = Regex.Matches(str, @"\s"); //t.e . [a-z] [A-Za-z] [0-9] ^ [A-Za-z0-9] \w \W \d \D \s \S
            foreach (Match item in mc)
            {
                Console.Write(item.Index+"  "+item.Value);
            }*/
            string words = "08/14/57 46 08/14/57 47 08/14/57 48 08/14/57 49";
            string regExp1 = "(\\s\\d{2}\\s)";
            MatchCollection matchSet = Regex.Matches(words,regExp1);
            foreach (Match item in matchSet)
            {
                Console.WriteLine(item.Groups[0].Captures[0]);
            }
            string regExp2 = "(?<dates>(\\d{2}/\\d{2}/\\d{2}))\\s";
            MatchCollection mc2 = Regex.Matches(words,regExp2);
            foreach (Match item in mc2)
            {
                Console.WriteLine(item.Groups["dates"]);
            }

            string str = "lions lion tigers tiger substring subss ss";
            string regExp3 = "\\w+(?=\\s)";
            MatchCollection mc3 = Regex.Matches(str,regExp3);
            foreach (Match item in mc3)
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine();

            string regExp4 = "\\b(?!sub)\\w+\\b";
            MatchCollection mc4 = Regex.Matches(str,regExp4);
            foreach (Match item in mc4)
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine();

            string regExp5 = "\\b\\w+(?<!s)\\b"; //"\\b\\w+(?<=s)\\b"
            MatchCollection mc5 = Regex.Matches(str, regExp5);
            foreach (Match item in mc5)
            {
                Console.WriteLine(item.Value);
            }
            Console.ReadLine();
        }
View Code

 

posted @ 2017-11-21 17:43  mikefts  阅读(157)  评论(0编辑  收藏  举报