我现在有一个字符串,里面东西内容很多,
“<link href="/images/style_0908.css" rel="stylesheet" type="text/css" />
<link href="/images/style_top.css" rel="stylesheet" type="text/css" />”
像这种href里地址是相对地址的,我现在要替换成绝对地址,变成为“<link href="http://www.baidu.com/images/style_0908.css" rel="stylesheet" type="text/css" />
<link href="http://www.baidu.com/images/style_top.css" rel="stylesheet" type="text/css" />”帮忙写下替换语句,谢谢各位大大了。参考:
(?<=href\s*=)(?:[ \s""']*)(?!#|mailto|location.|javascript|.*css|.*this\.)[^""']*(?:[ \s>""'])
这个正则自己试试,应该可以使用,我没用好。

解决方案 »

  1.   

    不会正则
    不过像这种规律这么明显的,用程序Replace也可以做到吧
      

  2.   

         string str=@"<link href=""images/style_0908.css"" rel=""stylesheet"" type=""text/css""/>";
               Console.Write(Regex.Replace(str,@"href=""([^>]*)""+([^>]*)",@"href=""http://www.baidu.com/$1"""));
      

  3.   

                string str = "<link href=\"/images/style_0908.css\" rel=\"stylesheet\" type=\"text/css\" />";
                str = Regex.Replace(str, @"<link\s+href=""([^""]+)""\s+rel=""stylesheet""\s+type=""text/css""\s*/>", "<link href=\"http://www.baidu.com$1\" rel=\"stylesheet\" type=\"text/css\" />");
                Console.WriteLine(str);
      

  4.   

    string str = "<link href=\"/images/style_0908.css\" rel=\"stylesheet\" type=\"text/css\" />";
    str = Regex.Replace(str, @"<link\s+href=""([^""]+)""\s+rel=""stylesheet""\s+type=""text/css""\s*/>", "<link href=\"http://www.baidu.com$1\" rel=\"stylesheet\" type=\"text/css\" />");
    Console.WriteLine(str);
      

  5.   


    string strContent = "<link href=\"/images/style_0908.css\" rel=\"stylesheet\" type=\"text/css\" />" + 
                             "<link href=\"/images/style_top.css\" rel=\"stylesheet\" type=\"text/css\" />";
                Regex re = new Regex( "(?<=<link href=\")[^/]*", RegexOptions.Multiline | RegexOptions.Compiled);
                strContent = re.Replace( strContent,"http://www.baidu.com");
      

  6.   

       string str="<link href=\"images/style_0908.css\" rel=\"stylesheet\" type=\"text/css\"/>";
               Console.Write(Regex.Replace(str, "<link[^>].*?href=\"([^>]*)\"+([^>]*)", "<link href=\"http://www.baidu.com/$1\""));
      

  7.   

    在msdn里查一下Regex.Replace的使用方法
      

  8.   

    偷懒的做法
            string s = " <link href='/images/style_0908.css' rel='stylesheet' type='text/css' />  <link href='/images/style_top.css' rel='stylesheet' type='text/css' />";
            s.Replace("/images", "http://www.baidu.com/images");
      

  9.   

    try...Regex reg = new Regex(@"(?is)(?<=href=(['""]?))(?!http://)[^'"">]+(?=\1)");
    string result = reg.Replace(yourStr, @"http://www.baidu.com$0");