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
  • 相关阅读:
    [BZOJ5338][TJOI2018]xor(可持久化Trie)
    [BZOJ4592][SHOI2015]脑洞治疗仪(线段树)
    [BZOJ4571][SCOI2016]美味(贪心+主席树)
    [BZOJ4570][SCOI2016]妖怪(凸包)
    [BZOJ4569][SCOI2016]萌萌哒(倍增+并查集)
    [BZOJ4567][SCOI2016]背单词(Trie+贪心)
    [BZOJ4565][HAOI2016]字符合并(区间状压DP)
    [BZOJ4561][JLOI2016]圆的异或并(扫描线)
    [BZOJ2650]积木
    [清橙A1210]光棱坦克
  • 原文地址:https://www.cnblogs.com/futengsheng/p/7874474.html
Copyright © 2011-2022 走看看