1. <a href="something.html">something</a>
2. <a href="http://www.domain.com/something.html">something</a>一段代码里有这2种链接,现在我要把第一种找出来,然后加上http变成第2种,请问怎么写?谢谢

解决方案 »

  1.   

    String regex="<a href=";String str="<a href=\"something.html\">something </a> "String finalStr = str.replaceAll(regex,"<a href=\"http://www.domain.com/")sysout(finalStr)
      

  2.   

    直接这样就可以了吧 String s1 = "<a href=\"something.html\">something </a>";
    String s2 = s1.replaceAll("href=\"", "href=\"http://www.domain.com/");
    System.out.println(s2);
      

  3.   

    获取<a href....></a>的正则表达式
    regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";可以先获取所有连接,再用replaceAll替换成你的需要的。 或者先替换在用正则表达式取链接。
      

  4.   


    import java.util.regex.*;
    //<a href="something.html">something </a> 
    //<a href="http://www.domain.com/something.html">something </a> public class Test66
    {
    public static void main(String... args) {
    StringBuilder sb = new StringBuilder();
    sb.append("<a href=\"http://www.domain.com/");
    String str = "<a href=\"something.html\">something </a>";
    Matcher m = Pattern.compile("<a href=\"(.*?)\">(.*?)</a>").matcher(str);
    while(m.find()) {
    sb.append(m.group(1));
    sb.append(">");
    sb.append(m.group(2));
    sb.append("</a>");
    }
    System.out.println(sb.toString());
    }
    }
      

  5.   

    public class Test{
        public static void main(String args[])  throws Exception{
        
            String regex="(<a[^>]*href=\")(?!\\w+://)(www)*";
            String str="<a href=\"something.html\">something</a> \n"
                      +"<a href=\"http://www.domain.com/something.html\">something </a>\n" 
                      +"<a href=\"ftp://www.domain.com/something.html\">something </a>\n"
                      +"<a href=\"www.something.html\">something</a>";
            String result = str.replaceAll(regex,"$1http://www");
            System.out.println(result); 
        }
    }结果:
    F:\java>java Test
    <a href="http://wwwsomething.html">something</a>
    <a href="http://www.domain.com/something.html">something </a>
    <a href="ftp://www.domain.com/something.html">something </a>
    <a href="http://www.something.html">something</a>
      

  6.   

    不太对,改为:
    public static void main(String args[])  throws Exception{
         //String str="12×(-3+12)-9/-2";
            //System.out.println(Arrays.toString(str.split("(?=[+×/()-])|((?<=[+×/()-])(?<![+×/(-]{2}))")));
            String regex="(<a[^>]*href=\")(?!\\w+://)(www\\.)*";
            String str="<a href=\"something.html\">something</a> \n"
                      +"<a href=\"http://www.domain.com/something.html\">something </a>\n" 
                      +"<a href=\"ftp://www.domain.com/something.html\">something </a>\n"
                      +"<a href=\"www.something.html\">something</a>";
            String result = str.replaceAll(regex,"$1http://www.");
            System.out.println(result); 
        }
      

  7.   

    F:\java>java Test
    <a href="http://www.something.html">something</a>
    <a href="http://www.domain.com/something.html">something </a>
    <a href="ftp://www.domain.com/something.html">something </a>
    <a href="http://www.something.html">something</a>
      

  8.   

    public class StringReplace {
        public static void main(String[] args) {
            String EMPTY_HREF = "href=\"([^(http)].*)\"";
            String FLL_HREF = "href=\"http://www.domain.com/$1\"";
            
            String text = "<a href=\"something.html\">something</a><a href=\"https://www.domain.com/something.html\">something</a>";
            text = text.replaceAll(EMPTY_HREF, FLL_HREF);
            System.out.println(text);
        }
    }自己写出来了,不过还是感谢大家