zoukankan      html  css  js  c++  java
  • 正则表达式提取案例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


            }
        }
    }

  • 相关阅读:
    个人永久性免费-Excel催化剂功能第18波-在Excel上也能玩上词云图
    个人永久性免费-Excel催化剂功能第17波-批量文件改名、下载、文件夹创建等
    个人永久性免费-Excel催化剂功能第16波-N多使用场景的多维表转一维表
    Excel催化剂开源第6波-Clickonce部署之自动升级瘦身之术
    Excel催化剂开源第5波-任务窗格在OFFICE2013中新建文档不能同步显示问题解决
    js中获取 table节点各tr及td的内容方法
    sql语句 怎么从一张表中查询数据插入到另一张表中?
    JS 如何获取自定义属性
    Ext.tree.TreePanel 属性详解
    Canvas 画圆
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2640578.html
Copyright © 2011-2022 走看看