110,535
社区成员
发帖
与我相关
我的任务
分享
using System.Text.RegularExpressions;
string str="<html><head> </html> <body> <a href= \"http://www.baidu.com \"> 百度 </a> <a href= \"http://www.taobao.com \"> 淘宝 </a> </body> </html>";
string RegexString = @"<a\s{0,}href= ""(?<link>.+?)"">(<linktext>.+?)</a>";
string[] link = GetRegValue(str, RegexString, "link", true);
string[] linktext = GetRegValue(str, RegexString, "linktext", true);// link和linktext就是你想要的了
/// <summary>
/// 正则表达式取值
/// </summary>
/// <param name="HtmlCode">源码</param>
/// <param name="RegexString">正则表达式</param>
/// <param name="GroupKey">正则表达式分组关键字</param>
/// <param name="RightToLeft">是否从右到左</param>
/// <returns></returns>
public string[] GetRegValue(string HtmlCode , string RegexString,string GroupKey,bool RightToLeft)
{
MatchCollection m;
Regex r;
if (RightToLeft == true)
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
}
else
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
m= r.Matches(HtmlCode);
string[] MatchValue = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
MatchValue[i] = m[i].Groups[GroupKey].Value;
}
return MatchValue;
}
using System.Text.RegularExpressions;
string str="<html><head> </html> <body> <a href= \"http://www.baidu.com \"> 百度 </a> <a href= \"http://www.taobao.com \"> 淘宝 </a> </body> </html>";
string RegexString = @"<a\s{0,}href= ""(?<link>.+?)"">.+?</a>";
string[] link = GetRegValue(str, RegexString, "link", true);
Response.Write(link.Length);
/// <summary>
/// 正则表达式取值
/// </summary>
/// <param name="HtmlCode">源码</param>
/// <param name="RegexString">正则表达式</param>
/// <param name="GroupKey">正则表达式分组关键字</param>
/// <param name="RightToLeft">是否从右到左</param>
/// <returns></returns>
public string[] GetRegValue(string HtmlCode , string RegexString,string GroupKey,bool RightToLeft)
{
MatchCollection m;
Regex r;
if (RightToLeft == true)
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
}
else
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
m= r.Matches(HtmlCode);
string[] MatchValue = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
MatchValue[i] = m[i].Groups[GroupKey].Value;
}
return MatchValue;
}