#region 处理动态Key的json对象
/// <summary>
/// 本类用于处理动态Key的json对象
/// </summary>
/// <param name="jObject">需要处理的json对象</param>
/// <param name="strJson"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns>json对象的第一个元素的values</returns>
public static string GetJsonValue(string strJson, out string[] name, out string[] value)
{
var jo = JObject.Parse(strJson);
var values = jo.Properties().Select(item => item.Value.ToString()).ToArray();
var keys = jo.Properties().Select(item => item.Name.ToString()).ToArray();
name = keys;
value = values;
var strResult = values[0];
return strResult;
}
#endregion
#region HTML字符串处理
/// <summary>
/// 返回匹配多个的集合值
/// </summary>
/// <param name="start">开始html tag</param>
/// <param name="end">结束html tag</param>
/// <param name="html">html</param>
///
public static IList<string> GetHtmls(string start, string end, string html)
{
IList<string> list = new List<string>();
try
{
string pattern = string.Format("{0}(?<g>(.|[\r\n])+?){1}", start, end);//匹配URL的模式,并分组
MatchCollection mc = Regex.Matches(html, pattern);//满足pattern的匹配集合
if (mc.Count != 0)
{
foreach (Match match in mc)
{
GroupCollection gc = match.Groups;
list.Add(gc["g"].Value);
}
}
}
catch { }
return list;
}
public static string GetHtml(string start, string end, string html)
{
string ret = string.Empty;
try
{
string pattern = string.Format("{0}(?<g>(.|[\r\n])+?)?{1}", start, end);//匹配URL的模式,并分组
ret = Regex.Match(html, pattern).Groups["g"].Value;
}
catch { }
return ret;
}
public static string funcGetSrc(string aText)
{
string pattern = @"(?is)<img (?:title=(['""]?)?(?<title>[^'""]*?)\1\s*|src=(['""]?)?(?<url>[^'""]*?)\2\s*)*/>";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(aText);
string imgname = "";
foreach (Match m in mc)
{
imgname += m.Groups[1].Value;
}
return imgname;
}
public static List<string> Get_A_Href(string html)
{
List<String> links = new List<String>();
//MatchCollection matches = Regex.Matches(html, "<a(?:\\s+.+?)*?\\s+href=\"([^\"]*?)\".+>(.*?)</a>", RegexOptions.IgnoreCase);
MatchCollection matches = Regex.Matches(html, @"(?is)<a[^>]*?href=(['""\s]?)(?<href>[^'""\s]*)\1[^>]*?>", RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
string s = match.Groups[1].Value;
links.Add(s);
}
return links;
}
/// <summary>
/// 获取字符中指定标签的值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="title">标签</param>
/// <returns>值</returns>
public static List<string> GetTitleContent(string str, string title)
{
string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title); //获取<title>之间内容
MatchCollection TitleMatchs = Regex.Matches(str, tmpStr, RegexOptions.IgnoreCase);
List<string> list = new List<string>();
if (TitleMatchs.Count != 0)
{
foreach (Match TitleMatch in TitleMatchs)
{
string result = TitleMatch.Groups["Text"].Value;
list.Add(result);
}
}
return list;
}
/// <summary>
/// 获取字符中指定标签的值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="title">标签</param>
/// <param name="attrib">属性名</param>
/// <returns>属性</returns>
public static List<string> GetTitleContent(string str, string title, string attrib)
{
string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", title, attrib); //获取<title>之间内容
MatchCollection TitleMatchs = Regex.Matches(str, tmpStr, RegexOptions.IgnoreCase);
List<string> list = new List<string>();
if (TitleMatchs.Count != 0)
{
foreach (Match TitleMatch in TitleMatchs)
{
string result = TitleMatch.Groups["url"].Value;
list.Add(result);
}
}
return list;
}
#endregion