学习参考:
1.掘金神文: https://juejin.im/post/582dfcfda22b9d006b726d11
2.辅助理解正则: https://regexper.com
经验
1.密码校验(6-20位,至少包含大小写和数字)
if (value) {
const regex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[^]{6,20}$");
if (!regex.test(this.form.getFieldValue("newPwd"))) {
callback("密码为6-20位,至少包含大小写和数字");
}
if (this.confirmDirty) {
form.validateFields(["confirm"], { force: true });
}
}
2.地址解析(手机, 电话, 姓名和地址)
package com.greatonce.oms.biz.impl.trade;
import com.greatonce.core.util.Assert;
import com.greatonce.core.util.StringUtil;
import com.greatonce.oms.biz.admin.RegionService;
import com.greatonce.oms.bo.admin.RegionMatchBO;
import com.greatonce.oms.bo.trade.SalesOrderModifyReceiverInfoBO;
import com.greatonce.oms.domain.admin.Region;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 地址信息解析器
*
* @author xinzhang
* @author Shenzhen Greatonce Co Ltd
* @version 2019/10/23
*/
@Component
public class AddressInfoParser {
private static final Pattern MOBILE_PATTERN = Pattern.compile("1[3456789]\\d{9}");
private static final Pattern PHONE_PATTERN = Pattern.compile("(\\d{2,4}-)?\\d{7,8}");
private static final Pattern PROVINCE_PATTERN = Pattern.compile("(.+省|.+自治区)|上海|北京|天津|重庆");
private static final Pattern MUNICIPALITY_PATTERN = Pattern.compile("上海|北京|天津|重庆");
private static final Pattern THREE_WORDS_SHORT_PROVINCE_PATTERN = Pattern.compile("内蒙古|黑龙江");
@Autowired
private RegionService regionService;
/**
* 解析地址信息
*
* @param info 地址示例:浙江省嘉兴市海宁市长安镇启潮路58号通顺万向轮集团5号仓库前门电梯 袁莎 18909934370 82455596
* @return SalesOrderModifyReceiverInfoBO 业务类
*/
public SalesOrderModifyReceiverInfoBO parseAddressInfo(String info) {
Assert.mustNotEmpty(info, "解析的信息不能为空");
SalesOrderModifyReceiverInfoBO bo = initSalesOrderModifyReceiverInfoBO();
// 解析手机
String mobile = parseMobile(info);
bo.setMobile(mobile);
info = info.replace(mobile, "");
// 解析电话
String phone = parsePhone(info);
bo.setTelephone(phone);
info = info.replace(phone, "");
// 解析地址
List<String> list = StringUtil.words(info);
list.sort((s1, s2) -> s2.length() - s1.length());
String addressInfo = list.get(0);
RegionMatchBO region = parseAddress(addressInfo);
if (!Assert.isNull(region)) {
if (!Assert.isNull(region.getCountry())) {
bo.setCountryId(region.getCountry().getRegionId());
bo.setCountryName(region.getCountry().getRegionName());
}
if (!Assert.isNull(region.getProvince())) {
bo.setProvinceId(region.getProvince().getRegionId());
bo.setProvinceName(region.getProvince().getRegionName());
}
if (!Assert.isNull(region.getCity())) {
bo.setCityId(region.getCity().getRegionId());
bo.setCityName(region.getCity().getRegionName());
}
if (!Assert.isNull(region.getDistrict())) {
bo.setDistrictId(region.getDistrict().getRegionId());
bo.setDistrictName(region.getDistrict().getRegionName());
}
bo.setAddress(region.getAddress());
}
//去掉地址剩下当名称
info = info.replace(list.get(0), "");
bo.setContact(info);
return bo;
}
/**
* 初始化SalesOrderModifyReceiverInfoBO,给默认值
*/
private SalesOrderModifyReceiverInfoBO initSalesOrderModifyReceiverInfoBO() {
SalesOrderModifyReceiverInfoBO bo = new SalesOrderModifyReceiverInfoBO();
bo.setContact("");
bo.setMobile("");
bo.setTelephone("");
bo.setAddress("");
bo.setCountryId(1L);
bo.setProvinceId(0L);
bo.setCityId(0L);
bo.setDistrictId(0L);
bo.setCountryName("中国");
bo.setProvinceName("");
bo.setCityName("");
bo.setDistrictName("");
return bo;
}
/**
* 解析手机
*/
private String parseMobile(String info) {
Matcher mobileMatcher = MOBILE_PATTERN.matcher(info);
if (mobileMatcher.find()) {
return mobileMatcher.group();
}
return "";
}
/**
* 解析电话(需要先排除手机号)
*/
private String parsePhone(String info) {
Matcher phoneMatcher = PHONE_PATTERN.matcher(info);
if (phoneMatcher.find()) {
return phoneMatcher.group();
}
return "";
}
/**
* 解析地址,返回地址全称
*/
private RegionMatchBO parseAddress(String addressInfo) {
// 省匹配
Matcher provinceMatcher = PROVINCE_PATTERN.matcher(addressInfo);
String matchProvinceName = "";
if (provinceMatcher.find()) {
matchProvinceName += provinceMatcher.group();
if (!MUNICIPALITY_PATTERN.matcher(matchProvinceName).matches()) {
addressInfo = addressInfo.replace(matchProvinceName, "");
}
} else {
// 匹配简称
String shortProvinceName = "";
String threeWordsMatch = addressInfo.substring(0, 3);
Matcher shortNameMatcher = THREE_WORDS_SHORT_PROVINCE_PATTERN.matcher(threeWordsMatch);
if (shortNameMatcher.find()) {
shortProvinceName += threeWordsMatch;
} else {
shortProvinceName += addressInfo.substring(0, 2);
}
Region country = regionService.getByNameAndLevel(0L, "中国", 1);
Region province = regionService.getByNameAndLevel(country.getRegionId(), shortProvinceName, 2);
if (Assert.isNull(province)) {
return null;
}
String regionName = province.getRegionName();
matchProvinceName += regionName;
String regionShortName = province.getRegionShortName();
if (addressInfo.contains(regionName)) {
addressInfo = addressInfo.replace(regionName, "");
} else if (addressInfo.contains(regionShortName)) {
addressInfo = addressInfo.replace(regionShortName, "");
} else {
return null;
}
}
String matchCityName;
// 市匹配
if (addressInfo.contains("市")) {
matchCityName = addressInfo.substring(0, addressInfo.indexOf("市") + 1);
} else {
Matcher cityMatcher = MUNICIPALITY_PATTERN.matcher(addressInfo);
if (cityMatcher.find()) {
matchCityName = cityMatcher.group();
} else {
return null;
}
}
addressInfo = addressInfo.replace(matchCityName, "");
String matchDistrict = "";
// 区匹配
if (addressInfo.contains("区")) {
matchDistrict = addressInfo.substring(0, addressInfo.indexOf("区") + 1);
} else if (addressInfo.contains("市")) {
matchDistrict = addressInfo.substring(0, addressInfo.indexOf("市") + 1);
} else if (addressInfo.contains("县")) {
matchDistrict = addressInfo.substring(0, addressInfo.indexOf("县") + 1);
}
addressInfo = addressInfo.replace(matchDistrict, "");
RegionMatchBO regionMatchBO = regionService.matchRegion("中国", matchProvinceName, matchCityName, matchDistrict);
regionMatchBO.setAddress(addressInfo);
return regionMatchBO;
}
}
基本规则