public
static
string
GetSingleTagValueByAttr(
string
inputstring,
string
tagName,
string
attrname,
string
key)
... {
Regex reg = new Regex("<" + tagName + " [^<>]*>", RegexOptions.IgnoreCase);
MatchCollection matchs = reg.Matches(inputstring);
string result = string.Empty;
foreach (Match match in matchs)
...{
string matchValue = match.Value;
Regex regValue = new Regex("content=".*"", RegexOptions.IgnoreCase);
if (matchValue.ToLower().IndexOf(attrname.ToLower() + "="" + key.ToLower() + """) != -1)
...{
if (regValue.IsMatch(matchValue))
...{
result = regValue.Match(matchValue).Value;
if (!string.IsNullOrEmpty(result))
...{
result = result.Replace("CONTENT=", "").Replace("content=","").Replace(""", "");
}
}
return result;
}
}
return null;
}
... {
Regex reg = new Regex("<" + tagName + " [^<>]*>", RegexOptions.IgnoreCase);
MatchCollection matchs = reg.Matches(inputstring);
string result = string.Empty;
foreach (Match match in matchs)
...{
string matchValue = match.Value;
Regex regValue = new Regex("content=".*"", RegexOptions.IgnoreCase);
if (matchValue.ToLower().IndexOf(attrname.ToLower() + "="" + key.ToLower() + """) != -1)
...{
if (regValue.IsMatch(matchValue))
...{
result = regValue.Match(matchValue).Value;
if (!string.IsNullOrEmpty(result))
...{
result = result.Replace("CONTENT=", "").Replace("content=","").Replace(""", "");
}
}
return result;
}
}
return null;
}
使用举例:
获取关键词 GetSingleTagValueByAttr(data, "meta", "name", "Keywords"); data是HTML源代码
获取描述GetSingleTagValueByAttr(data, "meta", "name", "Discription");