最近项目中需要根据前台需要识别的信息去做后台处理,根据地址识别出来姓名,手机号,以及地址信息返回给前端。我借用了一套算法: 这套算法是androi开发时候用的,我门java用的时候需要修改里面部分内容。
用的时候项目中也必须要有封装好的省、市、区(县信息 作为key,value为对应省市区编码 可以使用map封装。最好去网上搜 因为几千个地区。。。。
private void DiscernAddressInfo(String addressInfo) {
//解析需要的字段
String provinceInfo ="", cityInfo = "", countyInfo = "",areaInfo="", phoneInfo ="", nameInfo = "";
//记录字段的下标
int provinceIndex=-1,cityIndex=-1,areaIndex=-1,phoneIndex=-1,nameIndex=-1;
/*识别电话号码*/
Pattern pattern = Pattern.compile("([1][3-9][\\d]{9})|(0\\d{2,4}-\\d{7,8})");
Matcher matcher = pattern.matcher(addressInfo);
StringBuffer bf = new StringBuffer(64);
while (matcher.find()) {
bf.append(matcher.group()).append(",");
}
if (!StringUtil.isEmpty(phoneInfo)) {
phoneIndex=addressInfo.indexOf(phoneInfo);
addressInfo = addressInfo.replace(phoneInfo, "");//去掉已经识别的电话,防止加入详细地址
}
/*识别地址信息*/
List<String> proList = itemNameList(file.getSampleProvinceList()); //省份列表
for (String province : proList) {
String tem;
if (province.contains("自治区")) {
tem = province.replace("自治区","");
} else if (province.contains("省")) {
tem = province.replace("省","");
} else {
tem = province;
}
if (addressInfo.contains(tem)) {
provinceInfo = province;
provinceIndex = addressInfo.indexOf(tem);
//更新选择地址信息
this.province=province;
break;
}
}
if (!StringUtil.isEmpty(provinceInfo)) { //当输入框中有省份的时候
//根据省名称获取该省份编码
String provinceCode = file.getProvinceCode(provinceInfo);
//更新选择地址信息
this.provinceCode=provinceCode; //全局变量 自己再定义
//根据省编码获取市列表
List<String> cityList = itemNameList(file.getSampleCityList(provinceCode));
for (String city : cityList) {
String temCity = null;
if (city.contains("自治州")) {
temCity = city.replace("自治州","");
} else {
temCity = city.replace("市","");
}
if (addressInfo.contains(temCity)) {
cityInfo = city;
//更新选择地址信息
this.city=city;
break;
}
}
}
if (!StringUtil.isEmpty(cityInfo)) {//如果输入框包含市
//根据市名称、省份编码获取该市编码
String cityCode = file.getCityCode(cityInfo, file.getProvinceCode(provinceInfo));
//更新选择地址信息
this.cityCode=cityCode;
List<String> areaList = itemNameList(file.getSampleCountyList(cityCode));
for (String county : areaList) {
String countyTem = null;
if (county.contains("自治")) {
countyTem = county.replace("自治","");
}
countyTem = county.substring(0, county.length() - 1);
if (addressInfo.contains(countyTem)) {
countyInfo = county;
/*截取详细地址发字符*/
areaIndex = addressInfo.indexOf(countyTem)+countyTem.length();
if(areaIndex<=addressInfo.length())
{
areaInfo=addressInfo.substring(areaIndex,addressInfo.length());
}
//更新选择地址信息
this.county=county;
this.countyCode=file.getCountyCode(cityCode,county);
break;
}
}
}else{
if(addressInfo.indexOf("市")>0){
String cityTem=addressInfo.substring(addressInfo.indexOf("市")-2,addressInfo.indexOf("市")+1);
if(!TextUtils.isEmpty(file.getCountyCode(cityTem))){
this.countyCode=file.getCountyCode(cityTem);
if(!TextUtils.isEmpty(file.getCountyName(countyCode))){
this.county=file.getCountyName(countyCode);
this.cityCode=countyCode.substring(0,countyCode.length()-4);
this.city=file.getCityName(cityCode);
cityInfo=this.city;
countyInfo=this.county;
}
}
}
}
String areaTem = provinceInfo + cityInfo + countyInfo; //省市区
if (!StringUtil.isEmpty(countyInfo)&&addressInfo.contains(countyInfo)) {
areaIndex = addressInfo.indexOf(countyInfo)+countyInfo.length();
if(areaIndex<=addressInfo.length())
{
areaInfo=addressInfo.substring(areaIndex,addressInfo.length());
}
}
int cutNote = addressInfo.indexOf("备注");
/*识别姓名 姓名必须要在输入框首位才能识别。。*/
if (!StringUtil.isEmpty(provinceInfo)&&addressInfo.contains(provinceInfo)) {
provinceIndex = addressInfo.indexOf(provinceInfo);
}
if (provinceIndex > 0 && phoneIndex > 0) {
nameIndex = provinceIndex < phoneIndex ? provinceIndex : phoneIndex;
} else {
nameIndex = provinceIndex > phoneIndex ? provinceIndex : phoneIndex;
}
if (nameIndex > 0) {
String nameTem = addressInfo.substring(0, nameIndex);
if (nameTem.contains("姓名")) {
String[] nameTem1 = nameTem.split("姓名");
nameInfo = nameTem1[nameTem1.length - 1];
}
if (nameTem.contains("name")) {
String[] nameTem1 = nameTem.split("name");
nameInfo = nameTem1[nameTem1.length - 1];
}
if (StringUtil.isEmpty(nameInfo)) {
nameInfo = nameTem;
}
}
}