常用的正则表达式

【提取内容图片地址】

 1         /// <summary>
 2         /// 取得HTML中所有图片的 URL。
 3         /// </summary>
 4         /// <param name="sHtmlText">HTML代码</param>
 5         /// <returns>图片的URL列表</returns>
 6         public static string[] GetHtmlImageUrlList(string sHtmlText)
 7         {
 8             // 定义正则表达式用来匹配 img 标签
 9             Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
10 
11             // 搜索匹配的字符串
12             MatchCollection matches = regImg.Matches(sHtmlText);
13             int i = 0;
14             string[] sUrlList = new string[matches.Count];
15             // 取得匹配项列表
16             foreach (Match match in matches)
17                 sUrlList[i++] = match.Groups["imgUrl"].Value;
18             return sUrlList;
19         }

 

【去掉字符串中的数字】

 1         /// <summary>  
 2         /// 去掉字符串中的数字  
 3         /// </summary>  
 4         /// <param name="key"></param>  
 5         /// <returns></returns>  
 6         public string GetNumber(string key)
 7         {
 8             return Regex.Replace(key, @"([1-9]+[0-9]*|0)(\\.[\\d]+)?", "");
 9 
10         }

 

posted on 2016-03-30 16:35  算顺网  阅读(357)  评论(0编辑  收藏  举报

导航