正則表示式 匹配 區 縣 市 鎮 街道等
阿新 • • 發佈:2018-11-17
轉自:https://blog.csdn.net/superSubfn/article/details/80290491
package com.hvly.springjp_1.com.hlvy.test; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class AddressResolutionUtil { /** * 解析地址 * @author lin * @param address * @return */ public static List<Map<String,String>> addressResolution(String address){ String regex="(?<city>[^市]+自治州|.*?地區|.*?區|.+盟|市轄區|.*?市|.*?縣)(?<county>[^縣]+縣|.+區|.+市|.*?鎮|.+海域|.+島)?(?<village>.*)|(?<getcyv>^[\\u2E80-\\u9FFF]+$)"; Matcher m=Pattern.compile(regex).matcher(address); String city=null,county=null,town=null,village=null,getcyv=null; List<Map<String,String>> table=new ArrayList<Map<String,String>>(); Map<String,String> row=null; while(m.find()){ row=new LinkedHashMap<String,String>(); city=m.group("city"); row.put("city", city==null?"":city.trim()); county=m.group("county"); row.put("county", county==null?"":county.trim()); village=m.group("village"); row.put("village", village==null?"":village.trim()); getcyv=m.group("getcyv"); row.put("getcyv", getcyv==null?"":getcyv.trim()); table.add(row); } return table; } public static void main(String[] args) { System.out.println(addressResolution("上海市")); } }