2012-12-30 6 views
0

getWebsite()를 통해 bean에서 웹 사이트를 크롤링합니다. 나는 웹 사이트를 약간 시간 http://www.stackoverflow.com 얻고 언젠가는 http://stackoverflow.com를 얻는다. 내 질문에 대신 "빈 bean 대신에 setEmail()"[email protected] "로 바꿀 싶습니다. substring과 replaceAll 메서드의 도움으로 가능합니까? 부분 문자열을 가져오고 자바의 이전 텍스트로 바꿉니다

난 그냥 [email protected]로 문자열의 str.replaceAll()를 사용하여 http:// 또는 다른 가능한 패턴을 교체 확인 자신에게 거기에 힘든 시간을주지 않는다

String s=str.substring(0,11); 
    System.out.println("String s (0,11) :"+s); 
    String string=str.substring(0,7); 
    System.out.println("string (0,7):"+string); 
    String name=str.substring(11); 
    String name1=str.substring(7); 
    System.out.println("name :"+name); 
    boolean b=((!(s.length()==11)) || (string.length()==7))? true : ((!(string.length()==7)) || (s.length()==11))? false : true ; 
    System.out.println(b); 
    if(b==true) 
    { 
     System.out.println("condition TRUE"); 
     String replaceString=string.replaceAll(string,"[email protected]"); 
     System.out.println("replaceString :"+replaceString+name1); 
    } 
    if(b==false) 
    { 
     System.out.println("condition FALSE"); 
     String replaceString=s.replaceAll(s,"[email protected]"); 
     System.out.println("replaceString :"+replaceString+name); 
    } 
+2

미안 해요 대체 정규 표현식을 사용하는 것이 필요하지만,이 코드 난독 대회 우승자처럼 보인다. 너 정말로 무엇을하려고하는거야? – duffymo

+0

왜'URI'를 사용하지 않습니까? – fge

답변

0

우는 자신에 노력했다. 정규 표현식 잘

String str = "http://www.stackoverflow.com"; 

str = str.replaceAll("http:\\/\\/(www\\.)?","[email protected]"); 
System.out.println(str); 

http://rextester.com/SJD79549

을 내 질문은 권장 사용? 너 뭐하려고?

0

귀하의 경우에는

String orig = "Hello World!"; 

String repl = orig.substring(6, 11); // "World" 

String newstr = orig.replaceAll(repl, "user1937829"); // Hello user1937829! 

예, 당신은 orighttp://www.stackoverflow.com 또는 http://stackoverflow.com 경우

String newstr = orig.replaceAll("www", "").replaceAll("http://", "[email protected]"); 

newstrhttp://(www)[email protected]와 동일합니다 필요가 없습니다.

희망은 원하는대로 다.

0

당신은 함수

String str1= "http://www.stackoverflow.com"; 
String str2 = "http://stackoverflow.com"; 
System.out.println(str1.replaceAll("http:\\/\\/(www\\.)?","[email protected]")); 
System.out.println(str2.replaceAll("http:\\/\\/(www\\.)?","[email protected]"));