学习参考:
1.掘金神文: https://juejin.im/post/582dfcfda22b9d006b726d11
2.辅助理解正则: https://regexper.com

经验

1.密码校验(6-20位,至少包含大小写和数字)

  1. if (value) {
  2. const regex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[^]{6,20}$");
  3. if (!regex.test(this.form.getFieldValue("newPwd"))) {
  4. callback("密码为6-20位,至少包含大小写和数字");
  5. }
  6. if (this.confirmDirty) {
  7. form.validateFields(["confirm"], { force: true });
  8. }
  9. }

2.地址解析(手机, 电话, 姓名和地址)

  1. package com.greatonce.oms.biz.impl.trade;
  2. import com.greatonce.core.util.Assert;
  3. import com.greatonce.core.util.StringUtil;
  4. import com.greatonce.oms.biz.admin.RegionService;
  5. import com.greatonce.oms.bo.admin.RegionMatchBO;
  6. import com.greatonce.oms.bo.trade.SalesOrderModifyReceiverInfoBO;
  7. import com.greatonce.oms.domain.admin.Region;
  8. import java.util.List;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Component;
  13. /**
  14. * 地址信息解析器
  15. *
  16. * @author xinzhang
  17. * @author Shenzhen Greatonce Co Ltd
  18. * @version 2019/10/23
  19. */
  20. @Component
  21. public class AddressInfoParser {
  22. private static final Pattern MOBILE_PATTERN = Pattern.compile("1[3456789]\\d{9}");
  23. private static final Pattern PHONE_PATTERN = Pattern.compile("(\\d{2,4}-)?\\d{7,8}");
  24. private static final Pattern PROVINCE_PATTERN = Pattern.compile("(.+省|.+自治区)|上海|北京|天津|重庆");
  25. private static final Pattern MUNICIPALITY_PATTERN = Pattern.compile("上海|北京|天津|重庆");
  26. private static final Pattern THREE_WORDS_SHORT_PROVINCE_PATTERN = Pattern.compile("内蒙古|黑龙江");
  27. @Autowired
  28. private RegionService regionService;
  29. /**
  30. * 解析地址信息
  31. *
  32. * @param info 地址示例:浙江省嘉兴市海宁市长安镇启潮路58号通顺万向轮集团5号仓库前门电梯 袁莎 18909934370 82455596
  33. * @return SalesOrderModifyReceiverInfoBO 业务类
  34. */
  35. public SalesOrderModifyReceiverInfoBO parseAddressInfo(String info) {
  36. Assert.mustNotEmpty(info, "解析的信息不能为空");
  37. SalesOrderModifyReceiverInfoBO bo = initSalesOrderModifyReceiverInfoBO();
  38. // 解析手机
  39. String mobile = parseMobile(info);
  40. bo.setMobile(mobile);
  41. info = info.replace(mobile, "");
  42. // 解析电话
  43. String phone = parsePhone(info);
  44. bo.setTelephone(phone);
  45. info = info.replace(phone, "");
  46. // 解析地址
  47. List<String> list = StringUtil.words(info);
  48. list.sort((s1, s2) -> s2.length() - s1.length());
  49. String addressInfo = list.get(0);
  50. RegionMatchBO region = parseAddress(addressInfo);
  51. if (!Assert.isNull(region)) {
  52. if (!Assert.isNull(region.getCountry())) {
  53. bo.setCountryId(region.getCountry().getRegionId());
  54. bo.setCountryName(region.getCountry().getRegionName());
  55. }
  56. if (!Assert.isNull(region.getProvince())) {
  57. bo.setProvinceId(region.getProvince().getRegionId());
  58. bo.setProvinceName(region.getProvince().getRegionName());
  59. }
  60. if (!Assert.isNull(region.getCity())) {
  61. bo.setCityId(region.getCity().getRegionId());
  62. bo.setCityName(region.getCity().getRegionName());
  63. }
  64. if (!Assert.isNull(region.getDistrict())) {
  65. bo.setDistrictId(region.getDistrict().getRegionId());
  66. bo.setDistrictName(region.getDistrict().getRegionName());
  67. }
  68. bo.setAddress(region.getAddress());
  69. }
  70. //去掉地址剩下当名称
  71. info = info.replace(list.get(0), "");
  72. bo.setContact(info);
  73. return bo;
  74. }
  75. /**
  76. * 初始化SalesOrderModifyReceiverInfoBO,给默认值
  77. */
  78. private SalesOrderModifyReceiverInfoBO initSalesOrderModifyReceiverInfoBO() {
  79. SalesOrderModifyReceiverInfoBO bo = new SalesOrderModifyReceiverInfoBO();
  80. bo.setContact("");
  81. bo.setMobile("");
  82. bo.setTelephone("");
  83. bo.setAddress("");
  84. bo.setCountryId(1L);
  85. bo.setProvinceId(0L);
  86. bo.setCityId(0L);
  87. bo.setDistrictId(0L);
  88. bo.setCountryName("中国");
  89. bo.setProvinceName("");
  90. bo.setCityName("");
  91. bo.setDistrictName("");
  92. return bo;
  93. }
  94. /**
  95. * 解析手机
  96. */
  97. private String parseMobile(String info) {
  98. Matcher mobileMatcher = MOBILE_PATTERN.matcher(info);
  99. if (mobileMatcher.find()) {
  100. return mobileMatcher.group();
  101. }
  102. return "";
  103. }
  104. /**
  105. * 解析电话(需要先排除手机号)
  106. */
  107. private String parsePhone(String info) {
  108. Matcher phoneMatcher = PHONE_PATTERN.matcher(info);
  109. if (phoneMatcher.find()) {
  110. return phoneMatcher.group();
  111. }
  112. return "";
  113. }
  114. /**
  115. * 解析地址,返回地址全称
  116. */
  117. private RegionMatchBO parseAddress(String addressInfo) {
  118. // 省匹配
  119. Matcher provinceMatcher = PROVINCE_PATTERN.matcher(addressInfo);
  120. String matchProvinceName = "";
  121. if (provinceMatcher.find()) {
  122. matchProvinceName += provinceMatcher.group();
  123. if (!MUNICIPALITY_PATTERN.matcher(matchProvinceName).matches()) {
  124. addressInfo = addressInfo.replace(matchProvinceName, "");
  125. }
  126. } else {
  127. // 匹配简称
  128. String shortProvinceName = "";
  129. String threeWordsMatch = addressInfo.substring(0, 3);
  130. Matcher shortNameMatcher = THREE_WORDS_SHORT_PROVINCE_PATTERN.matcher(threeWordsMatch);
  131. if (shortNameMatcher.find()) {
  132. shortProvinceName += threeWordsMatch;
  133. } else {
  134. shortProvinceName += addressInfo.substring(0, 2);
  135. }
  136. Region country = regionService.getByNameAndLevel(0L, "中国", 1);
  137. Region province = regionService.getByNameAndLevel(country.getRegionId(), shortProvinceName, 2);
  138. if (Assert.isNull(province)) {
  139. return null;
  140. }
  141. String regionName = province.getRegionName();
  142. matchProvinceName += regionName;
  143. String regionShortName = province.getRegionShortName();
  144. if (addressInfo.contains(regionName)) {
  145. addressInfo = addressInfo.replace(regionName, "");
  146. } else if (addressInfo.contains(regionShortName)) {
  147. addressInfo = addressInfo.replace(regionShortName, "");
  148. } else {
  149. return null;
  150. }
  151. }
  152. String matchCityName;
  153. // 市匹配
  154. if (addressInfo.contains("市")) {
  155. matchCityName = addressInfo.substring(0, addressInfo.indexOf("市") + 1);
  156. } else {
  157. Matcher cityMatcher = MUNICIPALITY_PATTERN.matcher(addressInfo);
  158. if (cityMatcher.find()) {
  159. matchCityName = cityMatcher.group();
  160. } else {
  161. return null;
  162. }
  163. }
  164. addressInfo = addressInfo.replace(matchCityName, "");
  165. String matchDistrict = "";
  166. // 区匹配
  167. if (addressInfo.contains("区")) {
  168. matchDistrict = addressInfo.substring(0, addressInfo.indexOf("区") + 1);
  169. } else if (addressInfo.contains("市")) {
  170. matchDistrict = addressInfo.substring(0, addressInfo.indexOf("市") + 1);
  171. } else if (addressInfo.contains("县")) {
  172. matchDistrict = addressInfo.substring(0, addressInfo.indexOf("县") + 1);
  173. }
  174. addressInfo = addressInfo.replace(matchDistrict, "");
  175. RegionMatchBO regionMatchBO = regionService.matchRegion("中国", matchProvinceName, matchCityName, matchDistrict);
  176. regionMatchBO.setAddress(addressInfo);
  177. return regionMatchBO;
  178. }
  179. }

基本规则

image.png