package com.netmi.util;
import cn.hutool.core.util.StrUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AddressCuttingUtil {
public static void main(String[] args) {
System.out.println(addressDuplicate("上海市上海市浦东新区文化东路248号136"));
}
public static String addressDuplicate(String address) {
if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市") || address.startsWith("重庆市")) {
if (address.substring(3).startsWith("北京市") || address.substring(3).startsWith("天津市")
|| address.substring(3).startsWith("上海市") || address.substring(3).startsWith("重庆市")) {
return address.substring(3);
}
}
return address;
}
public static String addressCutting(String address) {
if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市") || address.startsWith("重庆市")) {
address = address.substring(0, 3) + "市辖区" + address.substring(3) + address.substring(4);
}
String regex = "(?<province>[^省]+省|.+自治区|[^澳门]+澳门|[^香港]+香港|[^市]+市)?(?<city>[^自治州]+自治州|[^特别行政区]+特别行政区|[^市]+市|.*?地区|.*?行政单位|.+盟|市辖区|[^县]+县)(?<county>[^县]+县|[^市]+市|[^镇]+镇|[^区]+区|[^乡]+乡|.+场|.+旗|.+海域|.+岛)(?<village>[^街道]+街道|[^镇]+镇|[^乡]+乡)?(?<address>.*)";
Matcher m = Pattern.compile(regex).matcher(address);
String province = null, city = null, county = null, town = null, village = null;
while (m.find()) {
province = m.group("province");
if (province.equals("北京市") || province.equals("天津市") || province.equals("上海市") || province.equals("重庆市")) {
city = province;
county = m.group("city");
if (county.split("区").length > 1) {
town = county.substring(county.indexOf("区") + 1);
county = county.substring(0, county.indexOf("区") + 1);
if (town.contains("区")) {
town = town.substring(county.indexOf("区") + 1);
}
} else {
county = m.group("county");
if (county.split("区").length > 1) {
town = county.substring(county.indexOf("区") + 1);
county = county.substring(0, county.indexOf("区") + 1);
}
}
} else {
city = m.group("city");
county = m.group("county");
if (county != null && !"".equals(county)) {
if (county.split("市").length > 1 && county.indexOf("市") < 5) {
town = county;
county = county.substring(0, county.indexOf("市") + 1);
town = town.substring(county.indexOf("市") + 1);
}
if (county.split("旗").length > 1) {
town = county;
county = county.substring(0, county.indexOf("旗") + 1);
town = town.substring(county.indexOf("旗") + 1);
}
if (county.split("海域").length > 1) {
town = county;
county = county.substring(0, county.indexOf("海域") + 2);
town = town.substring(county.indexOf("海域") + 2);
}
if (county.split("区").length > 1) {
town = county;
county = county.substring(0, county.indexOf("区") + 1);
town = town.substring(county.indexOf("区") + 1);
}
}
}
if (province != null && !"".equals(province)) {
province = province + "-";
}
if (city != null && !"".equals(city)) {
city = city + "-";
}
if (county != null && !"".equals(county)) {
county = county + "-";
}
village = m.group("village");
town = m.group("address");
}
if (StrUtil.isEmpty(province)) {
//普通地址
regex = "((?<provinceAndCity>[^市]+市|.*?自治州|.*?区|.*县)(?<county>[^区]+区|.*?市|.*?县|.*?路|.*?街|.*?道|.*?镇|.*?旗)(?<detailAddress>.*))";
m = Pattern.compile(regex).matcher(address);
while (m.find()) {
city = m.group("provinceAndCity");
county = m.group("county");
}
}
//精确到街道/乡/镇,如果要精确到具体地址 + town;
String newMachineAdress = province + city + county + village;
if (newMachineAdress != null && !"".equals(newMachineAdress)) {
newMachineAdress = newMachineAdress.replaceAll("null", "");
} else {
newMachineAdress = address;
}
return newMachineAdress;
}
}
java地址字符串解析省市区街道/乡/镇
最新推荐文章于 2024-03-29 13:23:13 发布