Quickie regular expression stuck
I have a line of stringy goodness:
"B8&soundFile=http%3A%2F%2Fwww.example.com%2Faeero%2Fj34d1.mp3%2Chttp%3A%2F%2Fwww.example.com%2Faudfgo%2set4.mp3"
Can I use regular expressions to just extract the http up to mp3 for all times it 开发者_Go百科exists?
I have tried reading the documents for regular expressions but none mention how to go FROM http to mp3. Can anyone help?
It would be better if you directly go for index based String operation.
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")));
Output :
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
I probably wouldn't do this with a regex. URL decode it, break it up by tokens, and parse it using Java's URL class.
Try http.+?mp3
the following should do it (assuming you want the http and mp3 as part of your match):
.*(http.*mp3)
if you just want the bits between then:
.*http(.*)mp3
for example:
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));
}
gives us
http%3A%2F%2Fwww.example.com%2Faudfgo%2set4.mp3
精彩评论