地址串中解析省市区信息

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 伍磊
*
*  从地址串中解析提取省市区
 */
public class AddressResolutionUtil {

    public static Map addressResolution(String address){
        String regex = "(?[^省]+自治区|.*?省|.*?行政区|.*?市)?(?[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?[^区]+区|.+镇)?(?.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province, city, district, town, village;
        Map map = new HashMap<>(16);
        while (m.find()) {
            province = m.group("province");
            map.put("province", province == null ? "" : province.trim());
            city = m.group("city");
            map.put("city", city == null ? "" : city.trim());
            district = m.group("district");
            map.put("district", district == null ? "" : district.trim());
            town = m.group("town");
            map.put("town", town == null ? "" : town.trim());
            village = m.group("village");
            map.put("village", village == null ? "" : village.trim());
        }
        return map;
    }
}

你可能感兴趣的:(地址串中解析省市区信息)