问题是这样的有如下2个HTML代码需要替换他们的URL,目的是是在URL之前加上 http://goto.com/?go=将 http://nba.com/up/index/2012/0906/index.css 替换成 http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css将 /f1/up/index/2012/0906/index.css ,替换成http://goto.com/?go=/f1/up/index/2012/0906/index.css 代码是在MSDN的例子的基础上修改得来的,目前可以做到第一个要求,但第二个需要URL中排除http://开通的URL不会写,已完成的代码如下 string testFullUrl = "<link href=\"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\">"; string testLocalUrl = "<link href=\"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\">"; static void DumpFullHrefs(String inputString)
{
Regex r = new Regex("href\\s*=\\s*(?:\"(?<1>https?://[^\"]*)\"|(?<1>https?://\\S+))",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
Console.WriteLine(r.Replace(inputString, "href=\"http://goto.com/?go=${1}\""));
}
{
Regex r = new Regex("href\\s*=\\s*(?:\"(?<1>https?://[^\"]*)\"|(?<1>https?://\\S+))",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
Console.WriteLine(r.Replace(inputString, "href=\"http://goto.com/?go=${1}\""));
}
string path = reg.Match(testFullUrl).ToString();
string res1 = testFullUrl.Replace(path, "http://goto.com/?go=" + path);
Console.WriteLine(res1);
static void Main(string[] args)
{
string testFullUrl = "<link href=\"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\">"; string testLocalUrl = "<link href=\"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\">"; Console.WriteLine(RepUrl(testFullUrl));
Console.WriteLine(RepUrl(testLocalUrl));
} public static string RepUrl(string url)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("(?<=href=\").*?(?=\")");
string path = reg.Match(url).ToString();
string res = url.Replace(url, "http://goto.com/?go=" + path);
return res;
}
Regex reg = new Regex(@"(?<=<link href="")");
string result = reg.Replace(testUrl, "http://goto.com/?go=");
richTextBox2.Text = result;
/*-----输出-----
<link href="http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css" rel="stylesheet"> <link href="http://goto.com/?go=/f1/up/index/2012/0906/index.css" rel="stylesheet">
*/
string testUrl = "<link href=\"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\"> <link href=\"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\">";
Regex regFullUrl = new Regex(@"(?<=<link href="")(?=http://)");
Regex regLocalUrl = new Regex(@"(?<=<link href="")(?!http://)");
richTextBox2.Text = "FullUrl:\n" + regFullUrl.Replace(testUrl, "http://goto.com/?go=") + "\n\nLocalUrl:\n" + regLocalUrl.Replace(testUrl, "http://goto.com/?go=");
/*-----输出-----
FullUrl:
<link href="http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css" rel="stylesheet"> <link href="/f1/up/index/2012/0906/index.css" rel="stylesheet">LocalUrl:
<link href="http://nba.com/up/index/2012/0906/index.css" rel="stylesheet"> <link href="http://goto.com/?go=/f1/up/index/2012/0906/index.css" rel="stylesheet">
*/
{
string testFullUrl = "<link href=\"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\">"; string testLocalUrl = "<link href=\"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\">"; Console.WriteLine(RepUrl(testFullUrl));
Console.WriteLine(RepUrl(testLocalUrl));
} public static string RepUrl(string url)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("(?<=href=\").*?(?=\")");
string path = reg.Match(url).ToString();
string res = url.Replace(path, "http://goto.com/?go=" + path);
return res;
}好像变量带错了
string testUrl = "<link href=\"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href= \"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href=http://nba.com/up/index/2012/0906/index.css rel=\"stylesheet\"> \n<link href=\"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href= \"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href=/f1/up/index/2012/0906/index.css rel=\"stylesheet\">";
Regex regFullUrl = new Regex(@"(?<=<link href=(?:\s*[""'])?)(?=https?://)");
Regex regLocalUrl = new Regex(@"((?><link href=(?:\s*[""'])?))(?!https?://)");
richTextBox2.Text = "FullUrl:\n" + regFullUrl.Replace(testUrl, "http://goto.com/?go=") + "\n\nLocalUrl:\n" + regLocalUrl.Replace(testUrl, "$1http://goto.com/?go=");
/*-----输出-----
FullUrl:
<link href="http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href=http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href="/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href=/f1/up/index/2012/0906/index.css rel="stylesheet">LocalUrl:
<link href="http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href=http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href="http://goto.com/?go=/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "http://goto.com/?go=/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href=http://goto.com/?go=/f1/up/index/2012/0906/index.css rel="stylesheet">
*/注意这次前一个替换成
"http://goto.com/?go="
而后一个替换成
"$1http://goto.com/?go="
而不是<link href = http://goto.com/?go=/up/index/2012/0906/index.css "stylesheet">麻烦帮忙再修改一下
string testUrl = "<link href=\"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href= \"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href=http://nba.com/up/index/2012/0906/index.css rel=\"stylesheet\"> \n<link href = http://nba.com/up/index/2012/0906/index.css rel=\"stylesheet\"> \n<link href=\"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href= \"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href=/f1/up/index/2012/0906/index.css rel=\"stylesheet\"> \n<link href = /f1/up/index/2012/0906/index.css rel=\"stylesheet\">";
Regex regFullUrl = new Regex(@"(?<=<link href\s*=\s*[""']?)(?=https?://)");
Regex regLocalUrl = new Regex(@"((?><link href\s*=\s*[""']?))(?!https?://)");
richTextBox2.Text = "FullUrl:\n" + regFullUrl.Replace(testUrl, "http://goto.com/?go=") + "\n\nLocalUrl:\n" + regLocalUrl.Replace(testUrl, "$1http://goto.com/?go=");
/*-----输出-----
FullUrl:
<link href="http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href=http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href = http://goto.com/?go=http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href="/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href=/f1/up/index/2012/0906/index.css rel="stylesheet">
<link href = /f1/up/index/2012/0906/index.css rel="stylesheet">LocalUrl:
<link href="http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href=http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href = http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href="http://goto.com/?go=/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "http://goto.com/?go=/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href=http://goto.com/?go=/f1/up/index/2012/0906/index.css rel="stylesheet">
<link href = http://goto.com/?go=/f1/up/index/2012/0906/index.css rel="stylesheet">
*/
string testUrl = "<link href=\"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href= \"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href=http://nba.com/up/index/2012/0906/index.css rel=\"stylesheet\"> \n<link href = http://nba.com/up/index/2012/0906/index.css rel=\"stylesheet\"> \n<link href=\"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href= \"/f1/up/index/2012/0906/index.css\" rel=\"stylesheet\"> \n<link href=/f1/up/index/2012/0906/index.css rel=\"stylesheet\"> \n<link href = /f1/up/index/2012/0906/index.css rel=\"stylesheet\">";
Regex regFullUrl = new Regex(@"(?<=<link href\s*=\s*[""']?)https?://[^'""\s>]+");
Regex regLocalUrl = new Regex(@"((?><link href\s*=\s*[""']?))(?!https?://)([^'""\s>]+)");
richTextBox2.Text = "FullUrl:\n" + regFullUrl.Replace(testUrl, delegate(Match m) { return System.Web.HttpUtility.UrlEncode("http://goto.com/?go=" + m.Value); }) + "\n\nLocalUrl:\n" + regLocalUrl.Replace(testUrl, delegate(Match m) { return m.Groups[1].Value + System.Web.HttpUtility.UrlEncode("http://goto.com/?go=" + m.Groups[2].Value); });
/*-----输出-----
FullUrl:
<link href="http%3a%2f%2fgoto.com%2f%3fgo%3dhttp%3a%2f%2fnba.com%2fup%2findex%2f2012%2f0906%2findex.css" rel="stylesheet">
<link href= "http%3a%2f%2fgoto.com%2f%3fgo%3dhttp%3a%2f%2fnba.com%2fup%2findex%2f2012%2f0906%2findex.css" rel="stylesheet">
<link href=http%3a%2f%2fgoto.com%2f%3fgo%3dhttp%3a%2f%2fnba.com%2fup%2findex%2f2012%2f0906%2findex.css rel="stylesheet">
<link href = http%3a%2f%2fgoto.com%2f%3fgo%3dhttp%3a%2f%2fnba.com%2fup%2findex%2f2012%2f0906%2findex.css rel="stylesheet">
<link href="/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "/f1/up/index/2012/0906/index.css" rel="stylesheet">
<link href=/f1/up/index/2012/0906/index.css rel="stylesheet">
<link href = /f1/up/index/2012/0906/index.css rel="stylesheet">LocalUrl:
<link href="http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href= "http://nba.com/up/index/2012/0906/index.css" rel="stylesheet">
<link href=http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href = http://nba.com/up/index/2012/0906/index.css rel="stylesheet">
<link href="http%3a%2f%2fgoto.com%2f%3fgo%3d%2ff1%2fup%2findex%2f2012%2f0906%2findex.css" rel="stylesheet">
<link href= "http%3a%2f%2fgoto.com%2f%3fgo%3d%2ff1%2fup%2findex%2f2012%2f0906%2findex.css" rel="stylesheet">
<link href=http%3a%2f%2fgoto.com%2f%3fgo%3d%2ff1%2fup%2findex%2f2012%2f0906%2findex.css rel="stylesheet">
<link href = http%3a%2f%2fgoto.com%2f%3fgo%3d%2ff1%2fup%2findex%2f2012%2f0906%2findex.css rel="stylesheet">
*/如果http://goto.com/?go=不编码,把它挪到UrlEncode外面就行了
睡觉去鸟,再有问题可就不管了