这里写自定义目录标题
- Java使用正则解析省市区/县 具体地址
- 问题场景
- 上demo
- 运行结果
Java使用正则解析省市区/县 具体地址
问题场景
OCR识别营业执照 获取详细地址并拆分
上demo
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestController {
public static void main(String[] args) {
System.out.println(resolveAddress("上海市上海市黄埔区xxx街道"));
System.out.println(resolveAddress("河南省郑州市中原区xxx村"));
System.out.println(resolveAddress("西藏自治区拉萨市当雄县xxx村"));
System.out.println(resolveAddress("中国香港香港特别行政区中西区xxx街道"));
System.out.println(resolveAddress("宁夏回族自治区银川市临武市xxx村"));
System.out.println(resolveAddress("青海省玉树藏族自治州玉树市xxx村"));
System.out.println(resolveAddress("海南省三沙市西沙群岛xxx村"));
System.out.println(resolveAddress("海南省琼海市嘉积镇xxx村"));
System.out.println(resolveAddress("海南省安定县定城镇xxx村"));
System.out.println(resolveAddress("海南省儋州市国营八一总场xxx地址"));
System.out.println(resolveAddress("海南省儋州市国营蓝洋农场xxx地址"));
System.out.println(resolveAddress("海南省五指山市常好乡xxx地址"));
System.out.println(resolveAddress("甘肃省临夏回族自治州临夏市xxx村"));
System.out.println(resolveAddress("新疆维吾尔自治区博尔塔拉蒙古自治州精河县xx村"));
System.out.println(resolveAddress("黑龙江省大兴安岭地区呼玛县xx村"));
System.out.println(resolveAddress("内蒙古自治区锡林郭勒盟西乌珠穆沁旗xx地址"));
}
private static Map resolveAddress(String address){
//1.地址的正则表达式
String regex = "(?<province>[^省]+省|.+自治区|[^澳门]+澳门|[^香港]+香港|[^市]+市)?(?<city>[^自治州]+自治州|[^特别行政区]+特别行政区|[^市]+市|.*?地区|.*?行政单位|.+盟|市辖区|[^县]+县)(?<county>[^县]+县|[^市]+市|[^镇]+镇|[^区]+区|[^乡]+乡|.+场|.+旗|.+海域|.+岛)?(?<address>.*)";
//2、创建匹配规则
Matcher m = Pattern.compile(regex).matcher(address);
String province;
String city;
String county;
String detailAddress;
Map<String,String> map = new HashMap<>(16);
while (m.find()){
//加入省
province = m.group("province");
map.put("province", province == null ? "" : province.trim());
//加入市
city = m.group("city");
map.put("city", city == null ? "" : city.trim());
//加入区
county = m.group("county");
map.put("county", county == null ? "" : county.trim());
//详细地址
detailAddress = m.group("address");
map.put("address", detailAddress == null ? "" : detailAddress.trim());
}
return map;
}
}
运行结果
"C:\Program Files\Java\jdk1.8.0_361\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:62106,suspend=y,server=n -javaagent:C:\dnydys\0.software\9.idea\ideaIC-2023.3.4.win\plugins\java\lib\rt\debugger-agent.jar=file:/C:/Users/17860/AppData/Local/Temp/capture.props -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_361\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_361\jre\lib\rt.jar;C:\1.work\0.java\pro-cli-main\pro-cli-main\target\classes;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-web\2.7.5\spring-boot-starter-web-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter\2.7.5\spring-boot-starter-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot\2.7.5\spring-boot-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-logging\2.7.5\spring-boot-starter-logging-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;C:\dnydys\0.software\11.maven\repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;C:\dnydys\0.software\11.maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;C:\dnydys\0.software\11.maven\repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;C:\dnydys\0.software\11.maven\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;C:\dnydys\0.software\11.maven\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\dnydys\0.software\11.maven\repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-json\2.7.5\spring-boot-starter-json-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.4\jackson-datatype-jdk8-2.13.4.jar;C:\dnydys\0.software\11.maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.4\jackson-datatype-jsr310-2.13.4.jar;C:\dnydys\0.software\11.maven\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.4\jackson-module-parameter-names-2.13.4.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.5\spring-boot-starter-tomcat-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.68\tomcat-embed-core-9.0.68.jar;C:\dnydys\0.software\11.maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.68\tomcat-embed-websocket-9.0.68.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-webmvc\5.3.23\spring-webmvc-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-actuator\2.7.5\spring-boot-starter-actuator-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-actuator-autoconfigure\2.7.5\spring-boot-actuator-autoconfigure-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-actuator\2.7.5\spring-boot-actuator-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\io\micrometer\micrometer-core\1.9.5\micrometer-core-1.9.5.jar;C:\dnydys\0.software\11.maven\repository\org\hdrhistogram\HdrHistogram\2.1.12\HdrHistogram-2.1.12.jar;C:\dnydys\0.software\11.maven\repository\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-aop\2.7.5\spring-boot-starter-aop-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\aspectj\aspectjweaver\1.9.7\aspectjweaver-1.9.7.jar;C:\dnydys\0.software\11.maven\repository\net\bytebuddy\byte-buddy\1.12.18\byte-buddy-1.12.18.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-validation\2.7.5\spring-boot-starter-validation-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.68\tomcat-embed-el-9.0.68.jar;C:\dnydys\0.software\11.maven\repository\org\hibernate\validator\hibernate-validator\6.2.5.Final\hibernate-validator-6.2.5.Final.jar;C:\dnydys\0.software\11.maven\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;C:\dnydys\0.software\11.maven\repository\org\jboss\logging\jboss-logging\3.4.3.Final\jboss-logging-3.4.3.Final.jar;C:\dnydys\0.software\11.maven\repository\mysql\mysql-connector-java\8.0.29\mysql-connector-java-8.0.29.jar;C:\dnydys\0.software\11.maven\repository\com\alibaba\druid-spring-boot-starter\1.2.14\druid-spring-boot-starter-1.2.14.jar;C:\dnydys\0.software\11.maven\repository\com\alibaba\druid\1.2.14\druid-1.2.14.jar;C:\dnydys\0.software\11.maven\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.7.5\spring-boot-autoconfigure-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-security\2.7.5\spring-boot-starter-security-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\security\spring-security-config\5.7.4\spring-security-config-5.7.4.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\security\spring-security-core\5.7.4\spring-security-core-5.7.4.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\security\spring-security-crypto\5.7.4\spring-security-crypto-5.7.4.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\security\spring-security-web\5.7.4\spring-security-web-5.7.4.jar;C:\dnydys\0.software\11.maven\repository\io\jsonwebtoken\jjwt\0.9.1\jjwt-0.9.1.jar;C:\dnydys\0.software\11.maven\repository\com\fasterxml\jackson\core\jackson-databind\2.13.4.2\jackson-databind-2.13.4.2.jar;C:\dnydys\0.software\11.maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;C:\dnydys\0.software\11.maven\repository\com\fasterxml\jackson\core\jackson-core\2.13.4\jackson-core-2.13.4.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-data-redis\2.7.5\spring-boot-starter-data-redis-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\data\spring-data-redis\2.7.5\spring-data-redis-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\data\spring-data-keyvalue\2.7.5\spring-data-keyvalue-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\data\spring-data-commons\2.7.5\spring-data-commons-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-oxm\5.3.23\spring-oxm-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\io\lettuce\lettuce-core\6.1.10.RELEASE\lettuce-core-6.1.10.RELEASE.jar;C:\dnydys\0.software\11.maven\repository\io\netty\netty-common\4.1.84.Final\netty-common-4.1.84.Final.jar;C:\dnydys\0.software\11.maven\repository\io\netty\netty-handler\4.1.84.Final\netty-handler-4.1.84.Final.jar;C:\dnydys\0.software\11.maven\repository\io\netty\netty-resolver\4.1.84.Final\netty-resolver-4.1.84.Final.jar;C:\dnydys\0.software\11.maven\repository\io\netty\netty-buffer\4.1.84.Final\netty-buffer-4.1.84.Final.jar;C:\dnydys\0.software\11.maven\repository\io\netty\netty-transport-native-unix-common\4.1.84.Final\netty-transport-native-unix-common-4.1.84.Final.jar;C:\dnydys\0.software\11.maven\repository\io\netty\netty-codec\4.1.84.Final\netty-codec-4.1.84.Final.jar;C:\dnydys\0.software\11.maven\repository\io\netty\netty-transport\4.1.84.Final\netty-transport-4.1.84.Final.jar;C:\dnydys\0.software\11.maven\repository\io\projectreactor\reactor-core\3.4.24\reactor-core-3.4.24.jar;C:\dnydys\0.software\11.maven\repository\org\reactivestreams\reactive-streams\1.0.4\reactive-streams-1.0.4.jar;C:\dnydys\0.software\11.maven\repository\com\baomidou\mybatis-plus-boot-starter\3.5.1\mybatis-plus-boot-starter-3.5.1.jar;C:\dnydys\0.software\11.maven\repository\com\baomidou\mybatis-plus\3.5.1\mybatis-plus-3.5.1.jar;C:\dnydys\0.software\11.maven\repository\com\baomidou\mybatis-plus-extension\3.5.1\mybatis-plus-extension-3.5.1.jar;C:\dnydys\0.software\11.maven\repository\com\baomidou\mybatis-plus-core\3.5.1\mybatis-plus-core-3.5.1.jar;C:\dnydys\0.software\11.maven\repository\com\baomidou\mybatis-plus-annotation\3.5.1\mybatis-plus-annotation-3.5.1.jar;C:\dnydys\0.software\11.maven\repository\com\github\jsqlparser\jsqlparser\4.3\jsqlparser-4.3.jar;C:\dnydys\0.software\11.maven\repository\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;C:\dnydys\0.software\11.maven\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-jdbc\2.7.5\spring-boot-starter-jdbc-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-jdbc\5.3.23\spring-jdbc-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\com\baomidou\mybatis-plus-generator\3.5.1\mybatis-plus-generator-3.5.1.jar;C:\dnydys\0.software\11.maven\repository\org\apache\velocity\velocity-engine-core\2.3\velocity-engine-core-2.3.jar;C:\dnydys\0.software\11.maven\repository\org\apache\commons\commons-lang3\3.12.0\commons-lang3-3.12.0.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-mail\2.7.5\spring-boot-starter-mail-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-context-support\5.3.23\spring-context-support-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\com\sun\mail\jakarta.mail\1.6.7\jakarta.mail-1.6.7.jar;C:\dnydys\0.software\11.maven\repository\com\sun\activation\jakarta.activation\1.2.2\jakarta.activation-1.2.2.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-freemarker\2.7.5\spring-boot-starter-freemarker-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\freemarker\freemarker\2.3.31\freemarker-2.3.31.jar;C:\dnydys\0.software\11.maven\repository\eu\bitwalker\UserAgentUtils\1.20\UserAgentUtils-1.20.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\boot\spring-boot-starter-quartz\2.7.5\spring-boot-starter-quartz-2.7.5.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\spring-tx\5.3.23\spring-tx-5.3.23.jar;C:\dnydys\0.software\11.maven\repository\org\quartz-scheduler\quartz\2.3.2\quartz-2.3.2.jar;C:\dnydys\0.software\11.maven\repository\com\mchange\mchange-commons-java\0.2.15\mchange-commons-java-0.2.15.jar;C:\dnydys\0.software\11.maven\repository\cn\hutool\hutool-all\5.8.11\hutool-all-5.8.11.jar;C:\dnydys\0.software\11.maven\repository\org\projectlombok\lombok\1.18.24\lombok-1.18.24.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-boot-starter\3.0.0\springfox-boot-starter-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-oas\3.0.0\springfox-oas-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\swagger\core\v3\swagger-annotations\2.1.2\swagger-annotations-2.1.2.jar;C:\dnydys\0.software\11.maven\repository\io\swagger\core\v3\swagger-models\2.1.2\swagger-models-2.1.2.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-spi\3.0.0\springfox-spi-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-schema\3.0.0\springfox-schema-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-core\3.0.0\springfox-core-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-spring-web\3.0.0\springfox-spring-web-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\github\classgraph\classgraph\4.8.83\classgraph-4.8.83.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-spring-webmvc\3.0.0\springfox-spring-webmvc-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-spring-webflux\3.0.0\springfox-spring-webflux-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-swagger-common\3.0.0\springfox-swagger-common-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\org\mapstruct\mapstruct\1.3.1.Final\mapstruct-1.3.1.Final.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-data-rest\3.0.0\springfox-data-rest-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-bean-validators\3.0.0\springfox-bean-validators-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-swagger2\3.0.0\springfox-swagger2-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\io\swagger\swagger-annotations\1.5.20\swagger-annotations-1.5.20.jar;C:\dnydys\0.software\11.maven\repository\io\swagger\swagger-models\1.5.20\swagger-models-1.5.20.jar;C:\dnydys\0.software\11.maven\repository\io\springfox\springfox-swagger-ui\3.0.0\springfox-swagger-ui-3.0.0.jar;C:\dnydys\0.software\11.maven\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\plugin\spring-plugin-core\2.0.0.RELEASE\spring-plugin-core-2.0.0.RELEASE.jar;C:\dnydys\0.software\11.maven\repository\org\springframework\plugin\spring-plugin-metadata\2.0.0.RELEASE\spring-plugin-metadata-2.0.0.RELEASE.jar;C:\dnydys\0.software\11.maven\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;C:\dnydys\0.software\9.idea\ideaIC-2023.3.4.win\lib\idea_rt.jar" com.echo.controller.TestController
Connected to the target VM, address: '127.0.0.1:62106', transport: 'socket'
{address=xxx街道, province=上海市, city=上海市, county=黄埔区}
{address=xxx村, province=河南省, city=郑州市, county=中原区}
{address=xxx村, province=西藏自治区, city=拉萨市, county=当雄县}
{address=xxx街道, province=中国香港, city=香港特别行政区, county=中西区}
{address=xxx村, province=宁夏回族自治区, city=银川市, county=临武市}
{address=xxx村, province=青海省, city=玉树藏族自治州, county=玉树市}
{address=xxx村, province=海南省, city=三沙市, county=西沙群岛}
{address=xxx村, province=海南省, city=琼海市, county=嘉积镇}
{address=xxx村, province=海南省, city=安定县, county=定城镇}
{address=xxx地址, province=海南省, city=儋州市, county=国营八一总场}
{address=xxx地址, province=海南省, city=儋州市, county=国营蓝洋农场}
{address=xxx地址, province=海南省, city=五指山市, county=常好乡}
{address=xxx村, province=甘肃省, city=临夏回族自治州, county=临夏市}
{address=xx村, province=新疆维吾尔自治区, city=博尔塔拉蒙古自治州, county=精河县}
{address=xx村, province=黑龙江省, city=大兴安岭地区, county=呼玛县}
{address=xx地址, province=内蒙古自治区, city=锡林郭勒盟, county=西乌珠穆沁旗}
Disconnected from the target VM, address: '127.0.0.1:62106', transport: 'socket'