2015-05-09 53 views
0

我試圖使用與與string.replace正則表達式替換URL使用正則表達式的網址和代碼如下查找和替換在java中

public class Test { 
    public static void main(String[] args) { 
     String test = "https://google.com"; 
     //String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; 
     String regex = "(http?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; // does not match <http://google.com> 

     String newText = test.replace(regex, ""); 
     System.out.println(newText); 
    } 
} 

我已經看着它幾個問題的SO,但它確實不能取代模式。有人可以告訴我,我怎麼做到這一點?

+0

您正則表達式匹配http://google.com,請確保你改變' http?'到'https?' –

+0

我沒有看到任何替換EMENT。你只是想刪除http://google.com? –

+0

@Pedro:非常抱歉。我被趕上了工作,所以這就是爲什麼。非常感謝你的幫助。下次不會再重複這個 –

回答

2

String.replace()不接受正則表達式。使用String.replaceAll代替:

String newText = test.replaceAll(regex, ""); 

至於正則表達式的問題,你應該匹配https還有:

String regex = "(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; 
2

不能使用正則表達式與replace,使用replaceAll代替,即:

String test = "something https://google.com something"; 
    try { 
     String newText = test.replaceAll("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", ""); 
     System.out.println(newText); 
    } catch (PatternSyntaxException ex) { 
     // Syntax error in the regular expression 
    } catch (IllegalArgumentException ex) { 
     // Syntax error in the replacement text (unescaped $ signs?) 
    } catch (IndexOutOfBoundsException ex) { 
     // Non-existent backreference used the replacement text 
    } 

產量:

something something 

現場演示:

http://ideone.com/Yi2hrb


正則表達式說明:

(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|] 

Options: Case sensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Default line breaks; Regex syntax only 

Match the regex below and capture its match into backreference number 1 «(https?|ftp|file)» 
    Match this alternative «https?» 
     Match the character string 「http」 literally «http» 
     Match the character 「s」 literally «s?» 
     Between zero and one times, as many times as possible, giving back as needed (greedy) «?» 
    Or match this alternative «ftp» 
     Match the character string 「ftp」 literally «ftp» 
    Or match this alternative «file» 
     Match the character string 「file」 literally «file» 
Match the character string 「://」 literally «://» 
Match a single character present in the list below «[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*» 
    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
    The literal character 「-」 «-» 
    A character in the range between 「a」 and 「z」 «a-z» 
    A character in the range between 「A」 and 「Z」 «A-Z» 
    A character in the range between 「0」 and 「9」 «0-9» 
    A single character from the list 「+&@#/%?=~_|!:,.;」 «+&@#/%?=~_|!:,.;» 
Match a single character present in the list below «[-a-zA-Z0-9+&@#/%=~_|]» 
    The literal character 「-」 «-» 
    A character in the range between 「a」 and 「z」 «a-z» 
    A character in the range between 「A」 and 「Z」 «A-Z» 
    A character in the range between 「0」 and 「9」 «0-9» 
    A single character from the list 「+&@#/%=~_|」 «+&@#/%=~_|» 
+0

小貼士:不要過度格式化和過度填充技術「垃圾」的帖子。你帖子中的所有「try ... catch」塊讓人們相信你自己懷疑你是否建議了一個正常工作的正則表達式。 –

+0

@stribizhev謝謝你的提示,但在Java中,這是處理錯誤的正確方法。 –

+1

但是,你的答案沒有被接受......不是第一次。因此,這只是我想與你分享的想法。 –