Search code examples
javaparsingurlzillow

How to parse a zillow URL in java


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"


Solution

  • 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