(.*?省|.*?市|.*?自治区)?(.*?市|.*?县|.*?区|.*?自治州)?(.*?县|.*?区)(.*?小区|.*?村)(.*?号楼|.*?楼|.*?栋|.*?组|.*?号)?(.*)
简单的正则规则如上,不是实际生产环境用的,实际用比这要复杂的多,
复制上面的正则,到https://regexper.com 看一下规则,展示效果如下:
按照自己项目里面的需要,调整的满足要求后复制到java代码里面使用,java代码
public static List<Map<String,String>> addressResolution(String address){
String regex="(?<province>.*?省|.*?市)?(?<city>.*?市|.*?县|.*?区)?(?<county>.*?市|.*?县|.*?区)(?<village>.*?小区|.*?村)(?<building>.*?号楼|.*?楼|.*?栋|.*?队|.*?组|.*?号|.*?室)?(?<other>.*)"; //不要直接用这个,这个是经过大量精简后的
String province=null, city=null,county=null,town=null,village=null,building=null,other=null;
List<Map<String,String>> table=new ArrayList<Map<String,String>>();
Map<String,String> row=null;
Matcher m=Pattern.compile(regex).matcher(address);
while(m.find()){
row=new LinkedHashMap<String,String>();
province=m.group("province");
row.put("province", province==null?"":province.trim());
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());
building=m.group("building");
row.put("building", building==null?"":building.trim());
other=m.group("other");
row.put("other", other==null?"":other.trim());
table.add(row);
}
return table;
}
测试
System.out.println(addressResolution("黑龙江省大庆市测试县测试镇测试村测试队11号"));