java后天实现登录功能代码_java后天地址(省,市,区)、姓名、手机号算法智能识别...

最近项目中需要根据前台需要识别的信息去做后台处理,根据地址识别出来姓名,手机号,以及地址信息返回给前端。我借用了一套算法: 这套算法是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 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 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 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;

}

}

}

觉得还不错。每个地方可以打个日志看一下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值