0

I need to get the "zpid" from the URL for example see the following link: http://www.zillow.com/homes/for_sale/Laie-HI/110560800_zpid/18901_rid/pricea_sort/21.70624,-157.843323,21.565342,-158.027859_rect/12_zm/

I need to get the value 110560800

I found URL Parser https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html but I could not find a way to get the "zpid"

user29768
  • 317
  • 4
  • 10

2 Answers2

0

You need to write a regular expression to match the group you want. In your case zpid is a number to match a number \d+ to be used

private static String extract(String url) { // http://www.zillow.com/homes/for_sale/Laie-HI/110560800_zpid/18901_rid/pricea_sort/21.70624,-157.843323,21.565342,-158.027859_rect/12_zm/
    Pattern pattern = Pattern.compile("(\\d+)_zpid");
    Matcher matcher = pattern.matcher(url);
    while (matcher.find()) {
        return matcher.group(1); //110560800
    }
    return null;
}

You can cast this String to number by using Integer.parseInt

Saravana
  • 12,647
  • 2
  • 39
  • 57
0

Here is how you can do:

String s = "http://www.zillow.com/homes/for_sale/Laie-HI/110560800_zpid/18901_rid/pricea_sort/21.70624,-157.843323,21.565342,-158.027859_rect/12_zm/";

        String[] url = s.split("/");//separating the string with delimeter "/" in url

        for(int i=0;i<url.length;i++){
            if(url[i].contains("zpid")){//go through each slit strings and search for keyword zpid
                String[] zpid = url[i].split("_");//if zpid is found, get the number part
                System.out.println(zpid[0]);

            }
        }