[原創]正則匹配

來源:互聯網
上載者:User
var strHref = top.document.location.href;
var regu = "OriginalPic";
var re = new RegExp(regu);
    if (strHref.search(re) != -1) {
       “have”;
    }

以上是JS的包含正則 ,search 確定一個字串是否包含另外一段字元

 

 

------------------------------------------------------------------

C#的Regex

 

代碼

html = "5800XM報價5480</a> <span>2008-11-11 </span></li><li><i>[<a href=\"http://yn.it168.com\">雲南</a>]</i> <a title=\"[昆明]昆明十一月份最受關注十大手機\" href=\"http://publish.it168.com/2008/1130/20081130005201.shtml\">[昆明]";

 string zhengze = "<div class=\"tit9\">(.+?)</div>";
Match m = Regex.Match(html, zhengze);  //正則匹配 
if (!string.IsNullOrEmpty(m.Groups[1].Value)){}

 

 去圖片的正則也可以簡化一下 <img\s[^<>]*?>

過濾所有HTML標籤

 

代碼

        html = System.Text.RegularExpressions.Regex.Replace(html, "&lt;[^&gt;]*&gt;", "", RegexOptions.IgnoreCase); 
            html = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", "", RegexOptions.IgnoreCase);  
            return html;

 

 

 過濾所有換行

 

代碼

        /// <summary>
        /// 別換行 
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        private static string NonrBr(string html)
        { 
            html = Regex.Replace(html, @"[\n\r]", "");
            html = html.TrimEnd((char[])"\n\r".ToCharArray());
            return html;
        }

 

 

 

還有一些常用的處理

 

代碼

  private static void regexReplaceFilter(ref string answerStr)
        {
            MatchCollection mc = null;
            string zhengZe = "";
            //去連結
            zhengZe = "<a[^>]*?href=[^>]*?>";
            mc = Regex.Matches(answerStr, zhengZe);
            foreach (Match var in mc)
            {
                answerStr = answerStr.Replace(var.Value, "");
            }
            //去圖片
            zhengZe = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
            mc = Regex.Matches(answerStr, zhengZe);
            foreach (Match var in mc)
            {
                answerStr = answerStr.Replace(var.Value, "");
            }
            //去網址
            zhengZe = @"http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>])*";
            mc = Regex.Matches(answerStr, zhengZe);
            foreach (Match var in mc)
            {
                answerStr = answerStr.Replace(var.Value, "");
            }
            //去QQ
            zhengZe = @"[1-9]\d{4,8}";
            mc = Regex.Matches(answerStr, zhengZe);
            foreach (Match var in mc)
            {
                answerStr = answerStr.Replace(var.Value, "");
            }
            //去電話  
            zhengZe = @"((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}";
            mc = Regex.Matches(answerStr, zhengZe);
            foreach (Match var in mc)
            {
                answerStr = answerStr.Replace(var.Value, "");
            }

            //去手機 
            zhengZe = @"((\(\d{3}\))|(\d{3}\-))?13\d{9}";
            mc = Regex.Matches(answerStr, zhengZe);
            foreach (Match var in mc)
            {
                answerStr = answerStr.Replace(var.Value, "");
            }
        }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.