地址处理工具类
可能不符合自己业务场景,需要自己结合业务进行训练调试修改
package com.zx.test.utils;
/**
* 地址处理工具类
*
* @author zx
* @date 2023/12/1
*/
public class AddressUtil {
private static final String SP_COMMA = ",";
private static final String SP_SPACE = " ";
private static final String SP_BLACK_LINE = "\\r?\\n";
private static final String SP_BLACK = "\\n";
private static final String SP_COLON = ":";
private static final String SP_SEMICOLON = ";";
/**
* 地址提取
*
* @param address
* @return
*/
public static AddressVo doProcessAddress(String address) {
if (address == null || address.isEmpty()) {
return null;
}
String[] datas = precutAddress(address);
if (datas.length < 2) {
return null;
}
//处理详细地址拆分
AddressVo ret = mateAddress(datas);
if (ret.getAreaName() == null && ret.getAddress() != null) {
String[] addressArr = AreaUtil.splitAddressAsAreaAndDetail(ret.getAddress());
if (addressArr != null && addressArr.length > 1) {
ret.setAreaName(addressArr[0]);
ret.setAddress(addressArr[1]);
}
}
if (ret.getAddress() == null && ret.getAreaName() != null) {
String[] addressArr = AreaUtil.splitAddressAsAreaAndDetail(ret.getAreaName());
if (addressArr != null && addressArr.length > 1) {
ret.setAreaName(addressArr[0]);
ret.setAddress(addressArr[1]);
}
}
return ret;
}
/**
* 匹配地址信息
*/
private static AddressVo mateAddress(String[] datas) {
AddressVo ret = new AddressVo();
for (String s : datas) {
if (s.contains(SP_COLON)) {
mateInfo(ret, s);
} else {
mateInfoWithoutColon(ret, s);
}
}
return ret;
}
private static final String regex = "^[\\u4e00-\\u9fa5]{2,4}$";
private static AddressVo mateInfoWithoutColon(AddressVo address, String input) {
//电话判断
if (isPhoneNumber(input)) {
address.setMobile(input);
return address;
}
// 2-4字姓名
if (input.matches(regex)) {
address.setName(input);
return address;
}
if(AreaUtil.isAreaName(input)){
address.setAreaName(input);
return address;
}
if(AreaUtil.isAddressDetail(input)){
address.setAddress(input);
return address;
}else{
if(address.getName()==null){
address.setName(input);
return address;
}
}
// System.out.println("UN PROCESS "+input);
return address;
}
private static final String REGEX = "^((13[0-9]|14[5|7]|15([0-3]|[5-9])|16[6]|17[0,1,3,5,6,7,8]|18[0-9]|19[8|9])\\d{8}|(\\d{3}\\d{4}))(,\\s*(13[0-9]|14[5|7]|15([0-3]|[5-9])|16[6]|17[0,1,3,5,6,7,8]|18[0-9]|19[8|9])\\d{8}|(\\d{3}\\d{4}))*$";
public static boolean isPhoneNumber(String number) {
return number.matches(REGEX);
private static final String[] PA_NAME = {"姓名", "人"};
private static final String[] PA_MOBILE = {"号码", "手机", "电话", "座机"};
private static final String[] PA_AREA = {"地区", "区域"};
private static final String[] PA_ADDRESS = {"地址", "详细"};
/**
* 存在:时,处理
*
* @param address
* @param input
*/
private static AddressVo mateInfo(AddressVo address, String input) {
String[] datas = input.split(SP_COLON);
for (String name : PA_NAME) {
if (datas[0].contains(name)) {
address.setName(datas[1]);
return address;
}
}
for (String name : PA_MOBILE) {
if (datas[0].contains(name)) {
address.setMobile(datas[1]);
return address;
}
}
for (String name : PA_AREA) {
if (datas[0].contains(name)) {
address.setAreaName(datas[1]);
return address;
}
}
for (String name : PA_ADDRESS) {
if (datas[0].contains(name)) {
//兼容地址直接带区域
String[] addressArr = AreaUtil.splitAddressAsAreaAndDetail(datas[1]);
if (addressArr != null && addressArr.length > 1) {
if(!addressArr[0].isEmpty() && (address.getAreaName()==null || address.getAreaName().isEmpty())) {
address.setAreaName(addressArr[0]);
}
if(!addressArr[1].isEmpty()) {
address.setAddress(addressArr[1]);
}
}else {
address.setAddress(datas[1]);
}
return address;
}
}
return address;
}
/**
* 拆分处理地址
*
* @param address
* @return
*/
private static String[] precutAddress(String address) {
address = convertToHalfWidth(address);
address = address.replaceAll(SP_COMMA, SP_SEMICOLON);
address = address.replaceAll(SP_BLACK_LINE, SP_SEMICOLON);
address = address.replaceAll(SP_BLACK, SP_SEMICOLON);
String[] addressArr = address.split(SP_SEMICOLON);
if (addressArr.length == 1) {
//无特殊符号分隔的,以空格二次拆分
addressArr = addressArr[0].split(SP_SPACE);
}
return addressArr;
}
/**
* 全角转半角
*
* @param input
* @return
*/
private static String convertToHalfWidth(String input) {
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 12288) { //全角空格转换为半角空格
chars[i] = (char) 32;
continue;
}
if (chars[i] >= 65280 && chars[i] <= 65374) { //其他全角字符转换为半角字符
chars[i] = (char) (chars[i] - 65248);
}
}
return new String(chars);
}
}
相关行政区划处理类AreaUtil
package com.zx.test.utils;
import java.util.regex.Pattern;
/**
* 行政区划代码处理类
*
* @author zx
*
*/
public class AreaUtil {
private static final String ZERO12 = "000000000000";
private static final String ZERO1 = "0";
/**
* 返回区县码
*
* @param areaCode
* @return
*/
public static String mark6AreaCode(String areaCode) {
if (areaCode != null && areaCode.length() > 6) {
return areaCode.substring(0, 6);
}
return areaCode;
}
/**
* 获取父区域码
* @param areaCode
* @return
*/
public static String markParentCode(String areaCode) {
if (areaCode == null || areaCode.length() < 3) {
return areaCode;
}
areaCode = markShortAreaCode(areaCode);
if (areaCode.length() > 3 && areaCode.length() < 7) {
return areaCode.substring(0, areaCode.length() - 2);
}
return areaCode.substring(0, areaCode.length() - 3);
}
/**
* 转换成12位区域代码
*
* @param areaCode
* @return
*/
public static String markLongAreaCode(String areaCode) {
if (areaCode != null && !areaCode.isEmpty()) {
return (areaCode + ZERO12).substring(0, 12);
}
return areaCode;
}
public static final String CHAR_SPACE = "";
/**
* 转换成12位区域代码
*
* @param areaCode
* @return
*/
public static String markShortAreaCode(String areaCode) {
if (areaCode != null && !areaCode.isEmpty()) {
if (!areaCode.endsWith(ZERO1)) {
return areaCode;
}
areaCode = areaCode.replaceAll("(0+)$", CHAR_SPACE);
switch (areaCode.length()) {
case 1:
return areaCode + ZERO1;
case 3:
return areaCode + ZERO1;
case 5:
return areaCode + ZERO1;
case 7:
return areaCode + ZERO1 + ZERO1;
case 8:
return areaCode + ZERO1;
case 10:
return areaCode + ZERO1 + ZERO1;
case 11:
return areaCode + ZERO1;
default:
return areaCode;
}
}
return areaCode;
}
public final static String ONE_AREA = "省,自治区,直辖市,特别行政区";
public final static String TWO_AREA = "市,地区,自治州,盟";
public final static String THREE_AREA = "县,县级市,自治县,区,旗,自治旗,特区,林区";
public final static String FOUR_AREA = "镇,乡,街道,民族乡,民族苏木,苏木,区公所";
public final static String[] SP_AREA = new String[]{"镇","街道","乡","民族乡","民族苏木","苏木","区公所","县","县级市","自治县","区","旗","自治旗","特区","林区","市","地区","自治州","盟","省","自治区","直辖市","特别行政区"};
private static final String SP_XQ = "区";;
public final static String[] SP_AREA_DETAIL = new String[]{"楼","栋","小区","村","单元","路","室"};
public static boolean isAddressDetail(String address){
for(String key : SP_AREA_DETAIL){
if(address.contains(key)){
return true;
}
}
return false;
}
public static boolean isAreaName(String address){
address = PATTERN_XQ.matcher(address).replaceAll(CHAR_SPACE);
System.out.println(address);
for(String key : SP_AREA){
if(address.contains(key)){
return true;
}
}
return false;
}
/**
* 拆分地址为两段
* 根据地址中的一二三四行政区进行拆分
* 从第四行政区开始拆分,逐步往前。
* @param address
* @return
*/
public static String[] splitAddressAsAreaAndDetail(String address){
for(String key : SP_AREA){
String[] ret = matchAddress(address,key);
if(ret!=null){
return ret;
}
}
return null;
}
private static final Pattern PATTERN_XQ = Pattern.compile("小区|[a-zA-Z]区|[0-9]区"); // 编译正则表达式,生成正则表达式对象
private static String[] matchAddress(String address ,String item){
if(SP_XQ.equals(item)){
int index = address.lastIndexOf(item);
if(index>0) {
String bstring = address.substring(0, index + item.length());
while (PATTERN_XQ.matcher(bstring).find()) {
index = address.lastIndexOf(item, index - 1);
bstring = address.substring(0, index + item.length());
}
}
if(index>0){
String[] ret = new String[2];
ret[0]=(address.substring(0, index+item.length()));
ret[1]=(address.substring(index+item.length()));
return ret;
}
return null;
}
int index = address.lastIndexOf(item);
if(index!=-1){
String[] ret = new String[2];
ret[0]=(address.substring(0, index+item.length()));
ret[1]=(address.substring(index+item.length()));
return ret;
}
return null;
}
}
相关VO提供
package com.zx.test.vo;
import lombok.Data;
@Data
public class AddressVo {
/**
* 姓名
*/
private String name;
/**
* 手机号
*/
private String mobile;
/**
* 区域代码
*/
private String areaCode;
/**
* 区域
*/
private String areaName;
/**
* 详细地址
*/
private String address;
}