//
//请问如何将下列字符串替换,如http://jingjia.baidu.com替换为../jingjia.baidu.com/index.html
//http://top.baidu.com替换为../top.baidu.com/index.html
//
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
public class Test { 
 
    public static void main(String args[]) { 
        String str = "</p><p style=height:14px>" + 
                "<a href=http://jingjia.baidu.com>企业推广</a> | " + 
                "<a href=http://top.baidu.com>搜索风云榜</a> | " + 
                "<a href=/home.html>关于百度</a> | " + 
                "<a href=http://ir.baidu.com>About Baidu</a>" + 
                "</p><p id=b>&copy;2008 Baidu " + 
                "<a href=http://www.baidu.com/duty>使用百度前必读</a> " + 
                "<a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a> " + 
                "<a href=http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412>" + 
                "<img src=http://gimg.baidu.com/img/gs.gif></a>" + 
                "</p></center></body></html><!--543ff95f18f36b11-->"; 
        String strin="hhh",tmp="www";
        int i=0;
        String  regex="(http://(.*?))[>]";
        Pattern pattern = Pattern.compile(regex); 
        Matcher matcher = pattern.matcher(str); 
        while(matcher.find()){ 
         tmp=matcher.group();
            System.out.println(tmp); 
            //strin=matcher.replaceAll("arg"+i);
           // strin=matcher.replaceFirst("arg"+i);
            i++;
        }  
        System.out.println(strin); 
        System.out.println("finish."); 
    } 
}

解决方案 »

  1.   

    [code=Java]String s = "http://jingjia.baidu.com".replace("http:/","../");[code]
      

  2.   

    String s = "http://jingjia.baidu.com".replace("http:/","../");
      

  3.   

    //请问如何将下列字符串替换,如http://jingjia.baidu.com 替换为../jingjia.baidu.com/index.html//http://top.baidu.com替换为../top.baidu.com/index.html 
    我是想使用正则表达式,从一个HTML文件中匹配一个URL就替换。
      

  4.   


    这一段应该怎么写??????while(matcher.find()){  
             tmp=matcher.group(); 
                System.out.println(tmp);  
                //strin=matcher.replaceAll("arg"+i); 
               // strin=matcher.replaceFirst("arg"+i); 
                i++; 
            }   
      

  5.   

    这种的替换成什么啊?http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412
      

  6.   

    http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412
    替换为
    ../www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412/index.html