原文:
java 从字符串中提取省、市、区、镇、乡等区域名称(包含少数民族地区)_java去掉城市后缀代码怎么写-CSDN博客https://blog.csdn.net/qq_40083897/article/details/83048587评论区说直辖市显示有问题,修改了一下
public String extractDistrict(String address) {
String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)?(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市)?(?<county>[^县区]+县|[^县区]+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
Matcher m = Pattern.compile(regex).matcher(address);
String county = null;
while (m.find()) {
county = m.group("county");
if (county != null) {
break;
}
}
return county;
}
要其他的原文有的,就修改了正则。