需要把地址字符串解析出来 比如广东省广州市白云区xxxx1洞,把省,市,区,详细地址解析出来。
public class DetailAddressUtil {
/**
* 省级地区不带省,市名称的地址
*/
public static List<String> provinceList = Arrays.asList(new String[]{"内蒙古", "广西壮族", "重庆", "西藏", "宁夏", "新疆", "香港", "澳门"});
/**
* 根据地址字符串解析成对象
*
* @param address
* @return
*/
public static Location getLocationByAddress(String address) {
Location location = new Location();
String province = null, city = null, county = null, town = null, village = null, detailedAddress = null;
if (StringUtils.isBlank(address)) {
return location;
}
String substring = address.substring(0, 4);
//香港新界单独处理
if (substring.equals("香港新界")) {
location.setProvince("香港");
location.setCity("新界");
String regex = "(?<county>[^县]+县|.+区|.+市|.+旗|.+委会|.+委员会|.+海域|.+岛)(?<village>.*)";
Matcher m = Pattern.compile(regex).matcher(address.substring(4));
while (m.find()) {
county = m.group("county");
location.setCounty(county == null ? "" : county.trim());
village = m.group("village");
detailedAddress = village == null ? "" : village.trim();
location.setDetailedAddress(detailedAddress);
}
return location;
}
//判断标志
Boolean isProvince = Boolean.TRUE;
for (String s : provinceList) {
if (substring.contains(s)) {
location.setProvince(s);
isProvince = Boolean.FALSE;
break;
}
}
//带省的城市
if (isProvince) {
String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?岛|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+委会|.+委员会|.+海域|.+岛)(?<village>.*)";
Matcher m = Pattern.compile(regex).matcher(address);
while (m.find()) {
province = m.group("province");
location.setProvince(province == null ? "" : province.trim());
city = m.group("city");
location.setCity(city == null ? "" : city.trim());
county = m.group("county");
location.setCounty(county == null ? "" : county.trim());
village = m.group("village");
detailedAddress = village == null ? "" : village.trim();
location.setDetailedAddress(detailedAddress);
}
} else {
//不带省的城市
String regex = "(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?岛|.*?行政区划|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+委会|.+委员会|.+海域|.+岛)(?<village>.*)";
Matcher m = Pattern.compile(regex).matcher(address.substring(location.getProvince().length()));
while (m.find()) {
city = m.group("city");
location.setCity(city == null ? "" : city.trim());
county = m.group("county");
location.setCounty(county == null ? "" : county.trim());
village = m.group("village");
detailedAddress = village == null ? "" : village.trim();
location.setDetailedAddress(detailedAddress);
}
}
return location;
}
@Data
public static class Location {
/**
* 省
*/
private String province;
/**
* 市
*/
private String city;
/**
* 县
*/
private String county;
/**
* 详细地址
*/
private String detailedAddress;
}
}
public static void main(String[] args) {
System.out.println(getLocationByAddress("内蒙古锡林郭勒盟正蓝旗xxx小区"));
System.out.println(getLocationByAddress("澳门离岛圣方济各堂区xxx小区"));
System.out.println(getLocationByAddress("香港新界北区xxx小区"));
System.out.println(getLocationByAddress("北京市北京市东城区xxx小区"));
System.out.println(getLocationByAddress("广东省阳江市阳西县xxx小区"));
}
DetailAddressUtil.Location(province=内蒙古, city=锡林郭勒盟, county=正蓝旗, detailedAddress=)
DetailAddressUtil.Location(province=澳门, city=离岛, county=圣方济各堂区, detailedAddress=)
DetailAddressUtil.Location(province=香港, city=新界, county=北区, detailedAddress=)
DetailAddressUtil.Location(province=北京市, city=北京市, county=东城区, detailedAddress=)
DetailAddressUtil.Location(province=广东省, city=阳江市, county=阳西县, detailedAddress=)