using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Common
{
public class RegHelper
{
//返回单个路径
public static string GetImgUrl(string HTMLStr)
{
string str = string.Empty;
//string sPattern = @"^<img/s+[^>]*>";
Regex r = new Regex(@"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>",
RegexOptions.Compiled);
Match m = r.Match(HTMLStr.ToLower());
if (m.Success)
str = m.Result("${url}");
return str;
}
//返回多个路径的情况
public static StringBuilder MyGetImgUrl(string text)
{
StringBuilder str = new StringBuilder();
string pat = @"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>";
Regex r = new Regex(pat, RegexOptions.Compiled);
Match m = r.Match(text.ToLower());
//int matchCount = 0;
while (m.Success)
{
Group g = m.Groups[2];
str.Append(g).Append(",");
m = m.NextMatch();
}
return str;
}
/// <summary>
/// 根据id过滤div内容
/// </summary>
/// <param name="html"></param>
/// <param name="id">id</param>
/// <param name="include_div_mask">是否包含div标记</param>
/// <returns></returns>
public static string GetDivByID(string html, string id, bool include_div_mask)
{
string regex_pattern = string.Format(include_div_mask ? @"(?is)<div[^>]*?id=""{0}.+?</div>" : @"(?is)(?<=<div[^>]*?id=""{0}[^>]+>).+?(?=</div>)", Regex.Escape(id));
Match m = Regex.Match(html, regex_pattern);
if (m.Success) return m.Value;
else return "";
}
}
}