zoukankan      html  css  js  c++  java
  • 常用的regex

    http://([\w-]+\.)+[\w-]+(/[\w-   ./?%&=]*)?  
    regex(@"http[s]?://([/\w-]+\.)+[\w-]+(/[/\w-   ./?%&=]*)?");
    http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?


    //##################提取url字符串
    System.Text.RegularExpressions.Regex regex=new Regex(@"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");       
                MatchCollection mc=regex.Matches(instr);
                if(mc.Count==1)
                {
                     ststr=mc[0].Value;
                }
                if(mc.Count>1)
                {
                     ststr=mc[1].Value;
                }

    //##################提取:  http://******.aspx              以^  http.//开头      中间符意字符  .+
                                                                                                  以.aspx结尾


    System.Text.RegularExpressions.Regex regext=new Regex(@"^http.//.+.aspx$");       
                MatchCollection mct=regext.Matches(ststr);
                if(mct.Count>0)
                {
                    temp=mct[0].Value;
                }




    //############################同理提取http://*********.html   和http://********.htm
                System.Text.RegularExpressions.Regex regexe=new Regex(@"^http.//.+.htm$");       
                MatchCollection mce=regexe.Matches(ststr);
                if(mce.Count>0)
                {
                    temp=mce[0].Value;
                }

               

                System.Text.RegularExpressions.Regex regexd=new Regex(@"^http.//.+.html$");       
                MatchCollection mcd=regexd.Matches(ststr);
                if(mcd.Count>0)
                {
                    temp=mcd[0].Value;
                }



    /////
    private void btn_count_Click(object sender, System.EventArgs e)
            {   
                string httpdate=GetHttpData(this.txt_url.Text);
                //<(?!tr|td)[^>]*>
                //<[^>]+>
                // strOutput = Replace(strOutput, "<", "&lt;")
                //strOutput = Replace(strOutput, ">", "&gt;")
                Regex r = new Regex(@"<(?!tr|td)[^>]*>");
                string ddd=r.Replace(httpdate,"").Replace("\r\n","").Replace("\t","");
                string dd1=ddd.Replace("<","").Replace(">","").Replace("\"","").Trim();
               
                Regex re=new Regex(@"^toptd.+webtrends$");
                Match mc=re.Match(dd1);
                string estr=mc.Value;
       
       

              }
            public string GetHttpData(string Url)
            {
                string sException=null;
                string sRslt=null;
                WebResponse oWebRps=null;
                WebRequest oWebRqst=WebRequest.Create(Url);
                oWebRqst.Timeout=50000;
                try
                {
                    oWebRps=oWebRqst.GetResponse();
                }
                catch(WebException e)
                {
                    sException=e.Message.ToString();
                    Response.Write(sException);
                }
                catch(Exception e)
                {
                    sException=e.ToString();
                    Response.Write(sException);
                }
                finally
                {
                    if(oWebRps!=null)
                    {
                        StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
                        sRslt=oStreamRd.ReadToEnd();
                        oStreamRd.Close();
                        oWebRps.Close();
                    }
                }
                return sRslt;
            }
           








    ///private void btn_count_Click(object sender, System.EventArgs e)
            {   
                string httpdate=GetHttpData(this.txt_url.Text);
                //<(?!tr|td)[^>]*>
                //<[^>]+>
                // strOutput = Replace(strOutput, "<", "&lt;")
                //strOutput = Replace(strOutput, ">", "&gt;")
    //            Regex r = new Regex(@"<(?!tr|td)[^>]*>");
    //            string ddd=r.Replace(httpdate,"").Replace("\r\n","").Replace("\t","");
    //            string dd1=ddd.Replace("<","").Replace(">","").Replace("\"","").Trim();
    //           
    //            Regex re=new Regex(@"^toptd.+webtrends$");
    //            Match mc=re.Match(dd1);
    //            string estr=mc.Value;
    //           
                string output=httpdate.Replace("\"","").Replace("\r\n","").Replace("\t","");
                Regex r=new Regex(@"<tr valign=top>.+</tr>");
               
                Match m=r.Match(output);
                string estr=m.Value;
           
    //            MatchCollection mc=r.Matches(estr);
    //           
    //            int  mc1=mc.Count;
    //            string af=mc[0].Value;
       

              }
            public string GetHttpData(string Url)
            {
                string sException=null;
                string sRslt=null;
                WebResponse oWebRps=null;
                WebRequest oWebRqst=WebRequest.Create(Url);
                oWebRqst.Timeout=50000;
                try
                {
                    oWebRps=oWebRqst.GetResponse();
                }
                catch(WebException e)
                {
                    sException=e.Message.ToString();
                    Response.Write(sException);
                }
                catch(Exception e)
                {
                    sException=e.ToString();
                    Response.Write(sException);
                }
                finally
                {
                    if(oWebRps!=null)
                    {
                        StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
                        sRslt=oStreamRd.ReadToEnd();
                        oStreamRd.Close();
                        oWebRps.Close();
                    }
                }
                return sRslt;
            }


    ///////////
        private void btn_count_Click(object sender, System.EventArgs e)
            {   
                string httpdate=GetHttpData(this.txt_url.Text);
       
                string output=httpdate.Replace("\"","").Replace("\r\n","").Replace("\t","");
                Regex r=new Regex(@"<tr valign=top>.+</tr>");
               

                /////////
                Match m=r.Match(output);
                string estr=m.Value;
                Regex nr=new Regex(@"<tr.+?>",RegexOptions.IgnoreCase);
                string newstr=nr.Replace(estr,"<tr>");
                Regex rr=new Regex(@"<a.+?>",RegexOptions.IgnoreCase);
                string nn=rr.Replace(newstr,"").Replace("</a>","");
                Regex rrr=new Regex(@"<td.+?>",RegexOptions.IgnoreCase);
                string nnn=rrr.Replace(nn,"<td>");
                //string nstr=nnn.Replace("<","").Replace(">","");
                string nnstr=nnn.Replace("<tr>","str").Replace("</tr>","etr").Replace("<td>","ftd").Replace("</td>","ltd");

                //
                ArrayList arr=new ArrayList();
               
                string [] arrstring=new string[100];
                string [] arrtdstring=new string[6];
                int startindex=0;
                int endindex=0;
               
                for(int i=0;i<nnstr.Length-2;i++)
                {
                    string tempstr=nnstr.Substring(i,3);
                    if(tempstr.StartsWith("str"))
                    {
                        startindex=i+3;

                    }
                    if(tempstr.StartsWith("etr"))
                    {
                        endindex=i-1;
                        int len=endindex-startindex+1;
                        string tempstring=nnstr.Substring(startindex,len);
                        arr.Add(nnstr.Substring(startindex,len));
                    }                            
                    if(arr.Count==3)
                    {
                       
                        break;
                    }
                }

                arr.CopyTo(arrstring ,0);
                System.Text.StringBuilder sb =new StringBuilder();
    //       
    //            for(int i=0;i<arrstring.Length;i++)
    //            {
    //       
    //                sb.Append(arrstring[i]+"\n\r");
    //            }
    //            Response.Write(sb.ToString());
    //           

                //列表项
    //            for(int j=0;j<3;j++)
    //            {
               
                    string tdstr=arrstring[0];
                    ArrayList arrtd=new ArrayList();
                    for(int i=0;i<tdstr.Length-2;i++)
                    {
                        string tempstr=tdstr.Substring(i,3);
                        if(tempstr.StartsWith("ftd"))
                        {
                            startindex=i+3;

                        }
                        if(tempstr.StartsWith("ltd"))
                        {
                            endindex=i-1;
                            int len=endindex-startindex+1;
                            string tempstring=tdstr.Substring(startindex,len);
                            arrtd.Add(tdstr.Substring(startindex,len));
                        }                            
                        if(arrtd.Count==6)
                        {
                            break;
                           
                        }
                    }
                    arrtd.CopyTo(arrtdstring,0);
                    //单项   
                    for(int k=0;k<arrtdstring.Length;k++)
                    {
                       
                        sb.Append(arrtdstring[k]+"<br>");
                    }
                   
               
    //            }
                    Response.Write(sb.ToString());
    //           
           

              }
            public string GetHttpData(string Url)
            {
                string sException=null;
                string sRslt=null;
                WebResponse oWebRps=null;
                WebRequest oWebRqst=WebRequest.Create(Url);
                oWebRqst.Timeout=50000;
                try
                {
                    oWebRps=oWebRqst.GetResponse();
                }
                catch(WebException e)
                {
                    sException=e.Message.ToString();
                    Response.Write(sException);
                }
                catch(Exception e)
                {
                    sException=e.ToString();
                    Response.Write(sException);
                }
                finally
                {
                    if(oWebRps!=null)
                    {
                        StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
                        sRslt=oStreamRd.ReadToEnd();
                        oStreamRd.Close();
                        oWebRps.Close();
                    }
                }
                return sRslt;
            }
           
           

  • 相关阅读:
    编写一个最原始的Servlet
    windows 通过cmd使用tail命令
    windows 配置jdk8环境变量
    Windows tomcat简单使用
    红黑树
    HashMap源码分析--jdk1.7
    IDEA debug断点调试技巧--转载
    idea 调试的时候变量的值变成了jar包显示
    浅谈Java中的hashcode方法--转载
    家庭记账本之微信小程序(八)
  • 原文地址:https://www.cnblogs.com/smallfa/p/1043197.html
Copyright © 2011-2022 走看看