问题是这样的有如下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}\""));
    }

解决方案 »

  1.   

                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("(?<=href=\").*?(?=\")");
                string path = reg.Match(testFullUrl).ToString();
                string res1 = testFullUrl.Replace(path, "http://goto.com/?go=" + path);
                Console.WriteLine(res1);
      

  2.   


            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;
            }
      

  3.   

    不是这样的,可能我前面没讲清楚,要求的是用2个表达式1个可以将 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 这2个URL的区别是一个有域名,一个没有域名,所以要分开处理
      

  4.   

    try...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 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">
    */
      

  5.   

    有必要了,这两种URL我还要分别再做处理的,前面没说明。如果用同一个正则表达式就没办法区分这两种URL了
      

  6.   


    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">
    */
      

  7.   

    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(path, "http://goto.com/?go=" + path);
                return res;
            }好像变量带错了
      

  8.   

    你的正则表达是没问题,但我需要的是将2中URL分开处理,前面有说过
      

  9.   

    这个可以,但希望能提高一下兼容性以便能处理下面的两种情况,一种是href之后的=号前后有空格的问题,还有一种是href之后的URL没有"号括起来,我之前的正则表达式就能处理这种情况,所以希望帮忙再改一下。string testBadUrl1 = "<link href = \"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\">";string testBadUrl1 = "<link href=https://nba.com/up/index/2012/0906/index.css \"stylesheet\">";
      

  10.   


    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="
      

  11.   

    基本上没问题,但不能处理href之后的=号前后有空格的这个URL,注意是=前后都有空格string testBadUrl1 = "<link href = \"http://nba.com/up/index/2012/0906/index.css\" rel=\"stylesheet\">";自己改了一下Regex regFullUrlRegex = new Regex(@"(?<=<link href\s*=(?:\s*[""'])?)(?=https?://)");后正常但regLocalUrl改成Regex regLocalUrlRegex = new Regex(@"((?><link href\s*=(?:\s*[""'])?))(?!https?://)");后替换处理下面的这个URL不正确string testBadUrl4 = "<link href = /up/index/2012/0906/index.css \"stylesheet\">";使用修改后的 regLocalUrlRegex 替换之后变成 <link href =http://goto.com/?go= /up/index/2012/0906/index.css "stylesheet">
    而不是<link href = http://goto.com/?go=/up/index/2012/0906/index.css "stylesheet">麻烦帮忙再修改一下
      

  12.   


    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">
    */
      

  13.   

    折腾了半天又多了一个问题,用正则表达式替换URL时有没有办法对取得URL地址进行编码?也就是将 http://nba.com/up/index/2012/0906/index.css 替换成 http://goto.com/?go=http%3a%2f%2fnba.com%2fup%2findex%2f2012%2f0906%2findex.css
      

  14.   

    正则不是来干这个的,编码你有Server.UrlEncode
      

  15.   

    我需要在替换的同时对URL进行编码,没有办法做到?
      

  16.   

    http://goto.com/?go=不编码?以下是参与编码的,注意正则和替换的内容都有变化
    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外面就行了
    睡觉去鸟,再有问题可就不管了
      

  17.   

    太好了,全部解决了,感动的眼泪鼻涕都流出来了。有点小问题http://goto.com/?go=是不用编码的,自己改了一下。你是我见过的最认真的版主,你将你们老板的Email给我,我发封Email给他要好好的把你表扬一下。