省市区字符串截取(正则)

切割省市区县字符串(只满足大部分正常的省市区)
public static List> getAddressInfo(String address) {
//1级 省 自治区 2级 市 自治州 地区 3级:区县市旗(镇?)
Map resultMap = new HashMap<>(4);

    String province=null,city=null,area=null,town=null,village=null;
    List> table=new ArrayList>();
    Map row=null;

    if (address.startsWith("香港特别行政区")) {
        resultMap.put("province","香港");
        table.add(resultMap);
        return table;
    } else if (address.contains("澳门特别行政区")) {
        resultMap.put("province","澳门");
        table.add(resultMap);
        return table;
    } else if (address.contains("台湾")) {
        resultMap.put("province","台湾");
        table.add(resultMap);
        return table;
    } else {
        //普通地址
        String regex="(?[^省]+省|.+自治区|.+市)(?[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?[^区]+区|.+镇|.+乡|.+街道|.+服务中心)?(?.*)";
        Matcher m=Pattern.compile(regex).matcher(address);
        while(m.find()){
            row=new LinkedHashMap();
            province=m.group("province");
            row.put("province", province==null?"":province.trim());
            city=m.group("city");
            row.put("city", city==null?"":city.trim());
            area=m.group("area");
            row.put("area", area==null?"":area.trim());
            town=m.group("town");
            row.put("town", town==null?"":town.trim());
            village=m.group("village");
            row.put("village", village==null?"":village.trim());
            table.add(row);
        }
        return table;
    }
}

你可能感兴趣的:(java)