2010-12-07 61 views
1

我有一個線拉絲善良的:晨間正則表達式卡住

"B8&soundFile=http%3A%2F%2Fwww.example.com%2Faeero%2Fj34d1.mp3%2Chttp%3A%2F%2Fwww.example.com%2Faudfgo%2set4.mp3" 

我可以使用正則表達式只提取HTTP高達MP3的存在的所有次?

我曾嘗試閱讀正則表達式的文檔,但沒有提到如何從http到mp3。誰能幫忙?

回答

1

如果您直接進行基於索引的字符串操作會更好。

String data = "B8&soundFile=http%3A%2F%2Fwww.example.com%2Faeero%2Fj34d1.mp3%2Chttp%3A%2F%2Fwww.example.com%2Faudfgo%2set4.mp3"; 

System.out.println(data.substring(data.indexOf("http"), data.indexOf(".mp3"))); 

輸出:

http%3A%2F%2Fwww.example.com%2Faeero%2Fj34d1 
B8&soundFile=http%3A%2F%2Fwww.example.com%2Faeero%2Fj34d1.mp3%2Chttp%3A%2F%2Fwww.example.com%2Faudfgo%2set4.mp3 
+0

!!!!!!!我爲什麼沒有想到這一點!現在試試這個。 Theres總是一個簡單的方法來做事情而不訴諸正則表達式。謝謝。 – David19801 2010-12-07 14:13:43

+0

@ David19801歡迎您來upvote&/ |標記爲答案:) – 2010-12-07 14:15:54

1

我可能不會用正則表達式做到這一點。 URL對它進行解碼,使用令牌將其分解,並使用Java's URL class對其進行解析。

0

以下應該這樣做(假設你想要的HTTP和MP3爲你的比賽的一部分):

.*(http.*mp3) 

如果你只是想接着之間的位:

.*http(.*)mp3 

例如:

String input = "B8&soundFile=http%3A%2F%2Fwww.example.com%2Faeero%2Fj34d1.mp3%2Chttp%3A%2F%2Fwww.example.com%2Faudfgo%2set4.mp3"; 
    Pattern p = Pattern.compile(".*(http.*mp3)"); 
    Matcher m = p.matcher(input); 
    if (m.find()) { 
     System.out.println(m.group(1)); 
    } 

給我們

http%3A%2F%2Fwww.example.com%2Faudfgo%2set4.mp3