• 正则表达式提取案例1


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Net;
    using System.IO;

    namespace 贪婪模式练习
    {
        class Program
        {
            static void Main(string[] args)
            {

                #region 贪婪模式

                //string msg = "1111。11。111。111111。";

                ////当使用限定符的时候,如果没有明确指定,则默认按照贪婪模式来匹配
                ////按照能匹配正则表达式的最多的字符串去匹配。
                //Match match = Regex.Match(msg, ".+");
                //if (match.Success)
                //{
                //    Console.WriteLine(match.Value);
                //}
                //Console.ReadKey();

                #endregion

                #region 终止贪婪模式

                // string msg = "1111。11。111。111111。";

                ////在【限定符的后面】加一个?表示终止贪婪模式
                // //zoo?
                // //当终止贪婪模式的时候,默认按照最少的方式来匹配。
                // Match match = Regex.Match(msg, ".+?");
                // if (match.Success)
                // {
                //     Console.WriteLine(match.Value);
                // }
                // Console.ReadKey();

                #endregion

                #region 案例1

                //string msg = "1111。11。111。111111。";
                ////.+  →  1111。11。111。111111
                ////。  → 。
                //Match match = Regex.Match(msg, ".+。");
                //if (match.Success)
                //{
                //    Console.WriteLine(match.Value);
                //}
                //Console.ReadKey();


                #endregion


                #region 案例2

                //string msg = "1111。11。111。111111。";

                ////.+?  →  1111
                ////。   →。
                ////1111。
                //Match match = Regex.Match(msg, ".+?。");
                //if (match.Success)
                //{
                //    Console.WriteLine(match.Value);
                //}
                //Console.ReadKey();


                #endregion

                #region 提取人名


                //string msg = "大家好。我们是S.H.E。我是S。我是H。呜呜。大家好我是杨中科。fffff";

                //MatchCollection matches = Regex.Matches(msg, "我是.+?。");
                //for (int i = 0; i < matches.Count; i++)
                //{
                //    Console.WriteLine(matches[i].Value);
                //    Console.WriteLine("==================================================");
                //}
                //Console.ReadKey();


                #endregion

                #region 下载网页上的图片
                string url = "http://localhost:8080/美女们.htm";
                WebClient webClient = new WebClient();//WebRequest\WebResponse
                webClient.Encoding = Encoding.GetEncoding("utf-8");
                string html = webClient.DownloadString(url);

                //在html这个字符串中提取所有的图片的路径
                //<img alt="" src="hotgirls/00_00.jpg" />
                //<img src="" alt="" title=""/>
                MatchCollection matches = Regex.Matches(html, "<[iI][mM][gG] alt=\"\" src=\"(.+)\" />");
                for (int i = 0; i < matches.Count; i++)
                {
                    if (matches[i].Success)
                    {
                        //Console.WriteLine(matches[i].Value);
                        //Console.WriteLine(matches[i].Groups[1].Value);
                        string imgUrl = Path.Combine("http://localhost:8080/", matches[i].Groups[1].Value);
                        webClient.DownloadFile(imgUrl, @"c:\美女\" + Path.GetFileName(matches[i].Groups[1].Value));
                        //Console.WriteLine(imgUrl);
                    }
                }
                Console.WriteLine("ok");
                Console.ReadKey();
                #endregion


            }
        }
    }

  • 相关阅读:
    一次性能测试的面试问题
    一次APP测试的感悟
    《程序员跳槽全攻略》读书笔记
    如果有人让你推荐编程技术书,请叫他看这个列表
    上班的一天
    马士兵Java视频教程 —— 学习顺序
    月薪3万的技术网站资源收集
    给32岁的自己一些答案
    《Vuser虚拟用户开发》读书笔记
    shell脚本异常:/bin/sh^M:bad interpreter: No such file or directory
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2640578.html
Copyright © 2020-2023  润新知