日期:2014-05-17  浏览次数:20431 次

求解网址转换的正则表达式
原字符串格式:
<loc>/Help/List-0018,0019.shtml</loc>
要求:
1>以.shtml .aspx为扩展名的超链接字符串
转换为目标网址:<loc>http://abc.sina.com.cn/Help/List-0018,0019.shtml</loc>

2>以.html .htm为扩展名的超链接字符串
转换为目标网址:<loc>http://www.sohu.com/Help/About Us.html</loc>
求解正则表达式,谢谢!

------解决方案--------------------
探讨
原字符串格式:
<loc>/Help/List-0018,0019.shtml</loc>
要求:
1>以.shtml .aspx为扩展名的超链接字符串
转换为目标网址:<loc>http://abc.sina.com.cn/Help/List-0018,0019.shtml</loc>

2>以.html .htm为扩展名的超链接字符串
转换为目标网址:<loc>http://www.sohu.com/Help/About Us.html</loc>

------解决方案--------------------
C# code

    string[] strarray = new string[] { @"<loc>/Help/List-0018,0019.shtml</loc>
",@"<loc>/Help/About Us.html</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(".html") || s.Contains(".htm"))
                {
                    string lstr = System.Text.RegularExpressions.Regex.Replace(s, @"(?<=<loc>).*?(?=/Help)", Replacestr[1]);
                    list.Add(lstr);
                }
            }
            list.ForEach(x => Response.Write(x));