原字符串格式:
<loc>/Help/List-0018,0019.shtml</loc>
要求:
1>以.shtml .aspx为扩展名的超链接字符串
转换为目标网址:<loc>http://abc.sina.com.cn/Help/List-0018,0019.shtml</loc>
2>以 .htm为扩展名的超链接字符串
转换为目标网址:<loc>http://www.sohu.com/Help/About Us</loc>
求解正则表达式,谢谢!
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
- C# code
string[] strarray = new string[] { @"<loc>/Help/List-0018,0019.shtml</loc>",@"<loc>/Help/About Us</loc>"}; string[] Replacestr = new string[] { @"http://abc.sina.com.cn",@"http://www.sohu.com"}; List<string> list = new List<string>(); foreach (string s in strarray) { if (s.Contains(".shtml") || s.Contains(".aspx")) { string lstr = System.Text.RegularExpressions.Regex.Replace(s, @"(?<=<loc>).*?(?=/Help)", Replacestr[0]); list.Add(lstr); } if (s.Contains("") || s.Contains(".htm")) { string lstr = System.Text.RegularExpressions.Regex.Replace(s, @"(?<=<loc>).*?(?=/Help)", Replacestr[1]); list.Add(lstr); } } list.ForEach(x => Response.Write(x));