static public string A(string str) { string[] spiID = new string[] { "</a>"}; string[] strSplit = str.Split(spiID, StringSplitOptions.None); int i = 0; string ret=""; foreach (string st in strSplit) { ret += st +spiID[0] +i.ToString(); i++; } return ret; }
string str = "<a href=\"http://www.sina.com.cn/\">新浪 </a> <a hre(太长,省略部份 - -#)
string pattern = "(<a[^>]*>[^<]*</a>)";Regex reg = new Regex(pattern);
MatchCollection matches = reg.Matches(str);for (int i = 0; i < matches.Count; i++)
{
string replace = "${1}" + (i + 1).ToString().PadLeft(2, '0');
str = reg.Replace(str, replace, 1, matches[i].Index);
}
Console.WriteLine(str);
Console.ReadLine();
{
string[] spiID = new string[] { "</a>"};
string[] strSplit = str.Split(spiID, StringSplitOptions.None);
int i = 0;
string ret="";
foreach (string st in strSplit)
{
ret += st +spiID[0] +i.ToString();
i++;
}
return ret;
}
string pattern = "
<a[^> ]*href=([ ' " "]?)(? <url> [^ ' " "> \s]*)\1?[^> ]*> (? <text> [^ <]*) </a>";
Regex reg = new Regex(pattern);
MatchCollection matches = reg.Matches(str);
int i=0;
string str="";s="";
foreach(Match mc in Matches)
{
s=(i + 1).ToString().PadLeft(2, '0');
str+=mc.Groups["Link"].Value.ToString().Trim()+Groups["Text"].Value.ToString().Trim()
+s;
i+=1;
}
Regex reg = new Regex(@"(?is)<a[^>]*>(?:(?!</?a\b).)*</a>");
string result = reg.Replace(yourStr, delegate(Match m) { return m.Value + (i++).ToString("00"); });