如何解析特定的URL以获取java中所需的值?

时间:2013-12-11 01:48:26

标签: java parsing url

我的网址链接在下方指定 -

http://newyork.citycompass.com/profile/756435/newyork_oh/time_warner_cable.html?publisher=test&q=time%20warner%2&Source=youtube&Match=b&Net=g&Dee=c&Devic=&Ad=1s6&A=3393&AdGRE=&Cont=&Ad=1101&UnID=195533

如何解析上述网址,以便在“个人资料”实体后获取值“756435”。

java新手

感谢任何帮助

3 个答案:

答案 0 :(得分:1)

我可能会使用这样的东西

String in = "http://newyork.citycompass.com/profile/756435/newy"; // cut...
String toMatch = "profile";
int pos = in.indexOf(toMatch);
String profileId = "";
if (pos > -1) {
  pos += toMatch.length();
  int pos2 = in.indexOf('/', pos + 2);
  if (pos2 > -1) {
    profileId = in.substring(pos + 1, pos2);
  }
}
System.out.println(profileId);

答案 1 :(得分:0)

您可以使用matcher查找http://newyork.citycompass.com/profile/这样的内容:

Pattern pattern = Pattern.compile("http://newyork.citycompass.com/profile/");
Matcher matcher = pattern.matcher(your_url_string_here);
if (matcher.find()){
    // If it is always 6 numbers
    String number = your_url_string_here.substring(matcher.end(),matcher.end()+6);
    //If it isn't always six numbers you could use indexOf to find the next / and then select between matcher.end() and the next slash.
}

答案 2 :(得分:0)

url.split("/")[4]

输出:

756435