92 lines
3.4 KiB
Java
92 lines
3.4 KiB
Java
package tms;
|
||
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
|
||
public class test {
|
||
|
||
public static String getCity(String address) {
|
||
// 先处理直辖市情况,直辖市的简称有"京"、"津"、"沪"、"渝"
|
||
String[] municipalityAbbreviations = {"京", "津", "沪", "渝"};
|
||
for (String abbreviation : municipalityAbbreviations) {
|
||
if (address.startsWith(abbreviation)) {
|
||
return cleanCityName(abbreviation + "市");
|
||
}
|
||
}
|
||
|
||
// 查找"省"字的位置
|
||
int provinceIndex = address.indexOf("省");
|
||
// 查找第一个"市"字的位置
|
||
int cityIndex = address.indexOf("市");
|
||
|
||
if (provinceIndex != -1 && cityIndex != -1 && cityIndex > provinceIndex) {
|
||
String city = address.substring(provinceIndex + 1, cityIndex + 1);
|
||
// 去除可能存在的前导符号(比如减号等)
|
||
city = cleanCityName(city);
|
||
return city;
|
||
} else if (cityIndex != -1) {
|
||
// 如果没有"省"字,直接返回第一个"市"字及其前面的部分作为城市名称,并去除前导符号
|
||
String city = address.substring(0, cityIndex + 1);
|
||
city = cleanCityName(city);
|
||
return city;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private static String cleanCityName(String cityName) {
|
||
// 使用正则表达式去除所有非汉字和非字母的字符
|
||
return cityName.replaceAll("[^\\u4e00-\\u9fa5a-zA-Z]", "");
|
||
}
|
||
|
||
public static String getCityFromAddress(String address) {
|
||
if (address == null || address.isEmpty()) {
|
||
return null;
|
||
}
|
||
|
||
// 分割地址为多个部分
|
||
String[] parts = address.split("-");
|
||
|
||
// 尝试从后往前查找城市名称
|
||
for (int i = parts.length - 1; i >= 0; i--) {
|
||
String part = parts[i].trim();
|
||
if (part.matches("[\\p{L}]+市|[\\p{L}]+自治区|[\\p{L}]+省")) {
|
||
return part;
|
||
}
|
||
}
|
||
|
||
// 如果没有找到完整的城市名称,尝试提取省份和城市
|
||
for (int i = parts.length - 2; i >= 0; i--) {
|
||
String cityPart = parts[i].trim();
|
||
String provincePart = parts[i + 1].trim();
|
||
if (provincePart.matches("[\\p{L}]+省") && cityPart.matches("[\\p{L}]+市")) {
|
||
return cityPart;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
|
||
public static void main(String[] args) {
|
||
String[] addresses = {
|
||
"南京市栖霞区龙潭镇建城路2号",
|
||
"常州市新北区宝塔山路36号",
|
||
"上海市宝山区蕴川路1867号",
|
||
"湖南省湘潭市雨湖区九华经济开发区白石西路6号舍弗勒(湘潭)有限公司南门",
|
||
"合肥市玉兰大道与香樟大道交口西南角合肥一期物流北门,22号厂房",
|
||
"重庆市璧山区变速器一厂",
|
||
"NY1_直送重庆九州共管库,重庆市两江新区王家街道重庆市渝北区两路寸滩保税港区空港功能区G区瑞月南路101号G02,重庆市,EMS_721098",
|
||
"广东省-东莞市-松山湖华为南方生产基地 C区-C10 H80",
|
||
"安徽省马鞍山市雨山区经济技术开发区龙山路199号马鞍山蓝黛传动机械有限公司"
|
||
};
|
||
|
||
for (String address : addresses) {
|
||
String city = getCityFromAddress(address);
|
||
System.out.println("地址: " + address + " - 城市: " + city);
|
||
}
|
||
}
|
||
|
||
|
||
}
|