zoukankan      html  css  js  c++  java
  • 正则

    正则表达式

    Regulator.exe
    元字符
    . 匹配任意单个字符
    * 匹配前面内容出现0或多次
    + 一或多次
    ? 0或一次
    [] 匹配[]中的字符出现一次[0-9]  [a-z]
    () 改变正则优先级  (zo)+ zo出现1或多次
    |  或   z|food 匹配z或food  (z|f)ood 匹配 zood或food
    {n} 出现n次
    {n,} 至少出现n次,最多不限
    {n,m} 至少出现n次,最多出现m次
    ^ 以谁开头    取反
    $ 以谁结尾
    d 代表一个数字 等同于[0-9]
    D 代表非数字 等同于[^0-9]
    s 代表换行符 Tab 制表符等空白字符
    w 匹配字母或数字或下划线或汉子 即能组成单词的字符
    W 非w  等同于[^w]
    ?<month> 分组 给组起名字

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;
    using System.Net;

    namespace Comp.WEB.正则
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

                //Regex 的一个  静态方法
              //1  Response.Write(Regex.IsMatch("55555",@"^d{5}$")) ; true
               

                //2字符串提取  Match 即是静态 又是 实例方法    匹配数组
                //Regex regex = new Regex(@"d+");
                //Match math=regex.Match("age=30");
                //if (math.Success)
                //{
                //    Response.Write(math.Value); //30

                //}

                //3    分组
                //Regex.Match("age=30", @"[a-z]", RegexOptions.IgnoreCase);//忽略大小写
                //Match math = Regex.Match("age=30", @"([a-zA-Z]+)=(d+)");   //()分组的意思
                //if (math.Success)
                //{
                //    Response.Write(math.Groups[0]); //返回的是整个整个正则表达式的结果  age=30
                //    Response.Write(math.Groups[1]); //age
                //    Response.Write(math.Groups[2]);      //30

                //}

               // 4  匹配路径 c:/a/b.txt   实际可以用Path.GetFileName更好
            //Match match= Regex.Match("c:/a/b.txt", @"^.+/(.+)$");
            //if (match.Success)
            //{
            //    Response.Write(match.Groups[1].Value); //b.txt
            //}

                //5   匹配 年月日   June    26,1951
                //string r=@"([a-zA-Z]+)s+(d{1,2}),s*(d{4})";
                //Match match=  Regex.Match(" June    26,1951", r);
                //if (match.Success)
                //{
                //    //Response.Write(match.Groups[0].Value);
                //    Response.Write(match.Groups[1].Value);  //June
                //    Response.Write(match.Groups[2].Value);        //26
                //    Response.Write(match.Groups[3].Value);            //1951
                //}

                //给分组七5.1
              //  ?<>分组
                //string r=@"(?<month>[a-zA-Z]+)s+(?<date>d{1,2}),s*(?<year>d{4})";
                //Match match=  Regex.Match(" June    26,1951", r);
                //if(match.Success)
                //{
                ////输出值的时候可以有二种方式输出
                //    Response.Write(match.Groups[1].Value);
                //    Response.Write(match.Groups["date"].Value);
                //}

                //截取ip地址
                //192.168.10.5[port=21,type=ftp]
                //string str = "192.168.10.5[port=21]";
                //string ip = "";
                //string port = "";
                //string type = "http";
                //string reg = @"(?<ip>[12]?d?d.[12]?d?d.[12]?d?d.[12]?d?d)[port=(?<port>d{1,5})(,type=(?<type>[a-zA-Z]+))?]";
                //Match match = Regex.Match(str, reg);
                //if (match.Success)
                //{
                //    ip = match.Groups["ip"].Value;
                //    port = match.Groups["port"].Value;
                //    if (!string.IsNullOrEmpty(match.Groups["type"].Value))
                //    {
                //        type = match.Groups["type"].Value;
                //    }
                //}
                //贪婪 .+    .*   
                //string str = "大家好。我是S.H.E。我22。我是H。我很好。呜呜。fffff";
                //str gex = @"我是(.+)。";//匹配句话  但是 他会一直多的往后匹配 匹配到   呜呜。
                //str gex2=@"我是(.+?)。"    //非贪婪模式

               // _提取多个值  1
                    //string str = "大家好,我是Hebe,我22岁了,身高180,我们团队有3个女女!";

                //MatchCollection matches = Regex.Matches(str,@"d+");
                //foreach (Match match in matches)
                //{
                //    if (match.Success)
                //    {
                //        Console.WriteLine(match.Value);
                //    }
                //}

                //提起多个值2

            //    string str = "大家好。我们是S.H.E。我是S。我是H。我是E。呜呜。fffff";

            //   MatchCollection matches =  Regex.Matches(str, @"我是(.+?)");
            //   foreach (Match match in matches)
            //   {
            //       if (match.Success)
            //       {
            //           Console.WriteLine(match.Groups[1].Value);
            //       }
            //   }

            //    Console.Read();
            //}

        // --------------------------------------       //从网站获取邮箱

                //从一个页面提取所有Email地址,用WebClient,自己动手写Email群发器

                //WebClient wc = new WebClient();
                // string content = wc.DownloadString("http://192.168.1.100/email.htm");


                // string reg = @">(?<mail>w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*)</a>";

                //MatchCollection matches = Regex.Matches(content, reg);
                //foreach (Match match in matches)
                //{
                //    if (match.Success)
                //    {
                //        Console.WriteLine(match.Groups["mail"].Value);
                //    }
                //}
                //--------------------------------

                //练习:从网从一个站抓取所有的图片地址,下载到硬盘

                // WebClient wc = new WebClient();
                // string content = wc.DownloadString("http://192.168.1.100/photos.htm");
                // string reg = @"style=""FLOAT:snone""stitle=.{8}sborder=0s+src=""(?<url>.+?)"">";

                //MatchCollection mc =  Regex.Matches(content, reg);
                //foreach (Match match in mc)
                //{
                //    //Console.WriteLine(match.Groups["url"].Value);

                //    string url = "http://192.168.1.100/" + match.Groups["url"].Value;

                //    Console.WriteLine(Path.GetFileName(url));   //下载
               
                //}


                ////练习:抓取所有超链接,特征:href="地址“
                //WebClient wc = new WebClient();
                //string content = wc.DownloadString("http://192.168.1.100/photos.htm");
                // string reg = @"http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?";
                //MatchCollection mc =   Regex.Matches(content, reg);

                //foreach (Match match in mc)
                //{
                //    if (match.Success)
                //    {
                //        Console.WriteLine(match.Value);
                //    }
                //}


                //==============第二种
                //string reg = @"align=middle><As+href=""(http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?)""><IMG";
                //MatchCollection mc = Regex.Matches(content,reg);
                //foreach (Match match in mc)
                //{
                //    if (match.Success)
                //    {
                //        Console.WriteLine(match.Groups[1].Value);
                //    }
                //}

                //练习:抓取新闻 采集工具
                //WebClient wc = new WebClient();
                //string reg = @"<divs+class=""qiushi_bodys+articles*""s+id=""w+""s+title="".+?"">(.+?)<ps+style=""float";
                //using (Stream stream = wc.OpenRead("http://192.168.1.100/qb.htm"))
                //{
                //    using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                //    {
                //        string content = sr.ReadToEnd();
                //        //单行模式匹配
                //        //让.可以匹配到

                //        MatchCollection mc = Regex.Matches(content, reg, RegexOptions.Singleline);
                //        foreach (Match match in mc)
                //        {
                //            if (match.Success)
                //            {
                //                string str = match.Groups[1].Value;
                //                //把html标签删除
                //                str = Regex.Replace(str, "<.+?>", "");
                //                str = str.Replace("&nbsp", "").Replace("(来自qiushibaike.com)", "");
                //                Console.WriteLine(str);
                //                Console.WriteLine("==============");
                //            }
                //        }
                //    }
                //}

                //正则替换
               // 1Regex.Replace
                //string str = "234-----233---34-3434";
                //str =  Regex.Replace(str,@"-+","-"); //多个-     //一个-
                //Console.WriteLine(str)          //234-233-3434


                  //2---------------   好多空格替换成一个空格
                //string str = "234       233  34 3434";
                //str = Regex.Replace(str,@" +"," ");
                //Console.WriteLine(str);

                  //3 将中文的双引号替换为英文双引号

               // string str = "string s1 = "red";string s2 = "blue"";
               // str = Regex.Replace(str, " “(.+?)”", ""$1"");
                                               //找到双引号里面的内容用一个组 包起来 不想他被替换 只是替换 外面的双引号
                                                //$1 匹配组里的内容             
                //Console.WriteLine(str);


                //3 将英文文的双引号替换为中文双引号

               //  string str = "string s1 = "red";string s2 = "blue"";
               //  str = Regex.Replace(str, " "(.+?)"", "“$1”");
               //// 找到双引号里面的内容用一个组 包起来 不想他被替换 只是替换 外面的双引号
               // //$1 匹配组里的内容             
               // Console.WriteLine(str);


                //4字符串前后替换
                //string str = "age=30";
                //str = Regex.Replace(str, @"(w+)=(d+)", "$2=$1");
                //Console.WriteLine(str);

                //5替换字符串格式 日期格式
                //string str = "我的生日是05/21/2010耶,他的生日05/21/2010,05/21/2010";

                //str =  Regex.Replace(str, @"(d{2})/(d{2})/(d{4})", "$3-$1-$2");
                                                                          //$3 第三组的内容
                //Console.WriteLine(str);

                //6      给一段 连接自动加上超链接
                //string str = "http://www.itcast.cn,,,,,,http://g.cn......http://www.qiushibaike.com";
                //str = Regex.Replace(str, @"(http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?)", "<a href='$1'>$1</a>");
                //Console.WriteLine(str);
                //Console.Read();
               

                //   ubb 转义
                //string str = "你好,[i]我发[/i]现一个[b]新网站[/b],[b]大家[/b]来看呀[url=http://www.qq.com]秋秋[/url],另外一个有时间也可以看看[url=http://www.rupeng.com]如鹏[/url],还有[url=http://www.itcast.cn]传智播客[/url]";

                //str = Regex.Replace(str, @"[[iI]](.+?)[/[iI]]", "<i>$1</i>");
                // 可以大写 小写
                //str = Regex.Replace(str, @"[[bB]](.+?)[/[bB]]", "<b>$1</b>");
                //str = Regex.Replace(str, @"[(url|URL)=(.+?)](.+?)[/(url|URL)]", "<a href='$2'>$3</a>");

                //Console.WriteLine(str);


                //Console.Rea
            }
        }
    }

  • 相关阅读:
    插入排序-Java
    选择排序-java
    逻辑回归----梯度上升
    logistic回归----- 随机梯度下降法
    JAVA实现聚类指标的计算Purity、NMI、RI、Precision、Recall、F值。
    Python 条形图绘制
    java中接口的注意事项
    算法-双向队列
    算法-manacher-最长回文子串-1
    算法-kmp-1
  • 原文地址:https://www.cnblogs.com/cdaq/p/3570383.html
Copyright © 2011-2022 走看看