.NET-14-贪婪模式|正则提取

贪婪模式:

贪婪模式在整个表达式匹配成功的前提下,尽可能多的匹配

("abbbbb", "a.*");

这里返回的结果是abbbbb,在a已经匹配成功的时候,会尽可能多的继续向后匹配

而非贪婪模式在整个表达式匹配成功的前提下,尽可能少的匹配

("abbbbb", "a.*?");

这里返回的是a,在匹配成功的时候就不在继续向后匹配了,按照最少的匹配结果来

种植贪婪模式就是在限定符(“{m,n}”、“{m,}”、“?”、“*”和“+”) 的后面增加一个?,这里的问号表示终止贪婪模式

结果变成这样“{m,n}?”、“{m,}?”、“??”、“*?”和“+?”

ps:如果?增加在表达式的后面表示该字符出现0次或者一次

比如"a?"表示a字符可以出现也可以不出现

正则提取:

 ////贪婪模式,在匹配成功的情况下尽可能多的匹配,返回结果是“abbbbb”
            //string str = "abbbbb";
            ////Regex.Match提取在指定的字符串中搜索指定的正则表达式第一个匹配项,返回结果为Match
            //Match mth = Regex.Match(str, "a.+");
            //Console.WriteLine(mth.Value);

            ////非贪婪模式,使用?终止贪婪模式,在匹配成功的情况下尽可能少的匹配,返回结果是“ab”
            //string str = "abbbbb";
            //Match mth = Regex.Match(str, "a.+?");
            //Console.WriteLine(mth.Value);

            ////非贪婪模式,什么都不返回
            //string str = "abbbbb";
            //Match mth = Regex.Match(str, ".*?");
            //Console.WriteLine(mth.Value);

            ////提取名称
            //string str = "大家好,我是SHE,我是大张伟,我是小张伟,我是快乐家族谢娜,我是*le,bulabulalalala";
            ////Regex.Matches提取在指定字符串中搜索指定的正则表达式的所有匹配项,这里需要使用终止贪婪模式,并且使用分组()
            //MatchCollection mats = Regex.Matches(str, "我是(.+?),");
            //foreach (Match item in mats)
            //{
            //    Console.WriteLine("匹配项:{0}-----------名称:{1}", item.Value, item.Groups[1].Value);
            //}

            ////从网页提取所有邮件地址
            ////创建一个对象
            //WebClient wc = new WebClient();
            ////下载指定界面为html字符串
            //string html = wc.DownloadString("https://www.baidu.com/");
            ////从html字符串获取邮件地址
            //MatchCollection mats = Regex.Matches(html, @"[-a-zA-Z0-9_.]+@[-a-zA-Z0-9]+(\.[a-zA-Z]+){1,2}");
            //foreach (Match item in mats)
            //{
            //    Console.WriteLine(item.Groups[1].Value);
            //}
            //Console.WriteLine("共{0}个邮箱", mats.Count);

            ////从网页提取图片
            ////创建一个对象
            //WebClient wc = new WebClient();
            ////下载指定的界面为html字符串
            //string html = wc.DownloadString("https://www.baidu.com/");
            ////从html字符串获取图片地址
            //MatchCollection mats = Regex.Matches(html, @"<img\s+alt="""" src=""(.+)"" />", RegexOptions.IgnoreCase);
            ////输出显示一下
            //foreach (Match item in mats)
            //{
            //    Console.WriteLine("匹配项:{0}-----------名称:{1}", item.Value, item.Groups[1].Value);
            //}
            ////下载图片
            //foreach (Match item in mats)
            //{
            //    //图片的路径
            //    string pathImg = "https://www.baidu.com/" + item.Groups[1].Value;
            //    //下载
            //    wc.DownloadFile(pathImg, @"d:\" + System.DateTime.Now.ToFileTime() + ".jpg");
            //}

            ////提取网页中所有链接
            ////创建对象
            //WebClient wc = new WebClient();
            ////下载界面为html字符串
            //string html = wc.DownloadString("https://www.baidu.com/");
            //MatchCollection mats = Regex.Matches(html, @"<a\s*href=""(.+?)"">.+</a> ", RegexOptions.IgnoreCase);
            //foreach (Match item in mats)
            //{
            //    Console.WriteLine("匹配项:{0}-----------名称:{1}", item.Value, item.Groups[1].Value);
            //}

猜你喜欢

转载自blog.csdn.net/m0_37532448/article/details/81215796