zoukankan      html  css  js  c++  java
  • 正则

            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
  • 相关阅读:
    JavaScript-Runoob-JS 参考手册 :JavaScript 对象
    JavaScript-Runoob-JS 实例 :JavaScript 总结
    JavaScript-Runoob-JS 实例 :JavaScript HTML DOM 实例
    JavaScript-Runoob-JS 实例 :JavaScript Browser 对象实例
    JavaScript-Runoob-JS 实例 :JavaScript 对象实例
    linux 下 php 安装 libevent
    linux 下 php 安装 event
    mybatis实战教程(mybatis in action),mybatis入门到精通
    将MapReduce的结果输出至Mysql数据库
    spring路径通配符
  • 原文地址:https://www.cnblogs.com/futengsheng/p/7874474.html
Copyright © 2011-2022 走看看