输出
代码实现
import java. util. regex. Matcher ;
import java. util. regex. Pattern ;
public class AddressResolutionUtil {
public static String addressResolution ( String address) {
String regex= "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)" ;
Matcher m= Pattern . compile ( regex) . matcher ( address) ;
String province= null , city= null , county= null , town= null , village= null ;
StringBuilder stringBuilder = new StringBuilder ( ) ;
while ( m. find ( ) ) {
province= m. group ( "province" ) ;
System . out. println ( province) ;
stringBuilder. append ( province== null ? "" : province. trim ( ) ) ;
city= m. group ( "city" ) ;
System . out. println ( city) ;
stringBuilder. append ( city== null ? "" : city. trim ( ) ) ;
county= m. group ( "county" ) ;
System . out. println ( county) ;
stringBuilder. append ( county== null ? "" : county. trim ( ) ) ;
town= m. group ( "town" ) ;
System . out. println ( town) ;
stringBuilder. append ( town== null ? "" : town. trim ( ) ) ;
village= m. group ( "village" ) ;
System . out. println ( village) ;
stringBuilder. append ( village== null ? "" : village. trim ( ) ) ;
}
System . out. println ( stringBuilder) ;
return stringBuilder. toString ( ) ;
}
public static String addressResolution1 ( String address) {
String regex= "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)" ;
Matcher m= Pattern . compile ( regex) . matcher ( address) ;
String province= null , city= null , county= null , town= null , village= null ;
StringBuilder stringBuilder = new StringBuilder ( ) ;
while ( m. find ( ) ) {
province= m. group ( "province" ) ;
stringBuilder. append ( province== null ? "" : province. trim ( ) ) . append ( "/" ) ;
city= m. group ( "city" ) ;
stringBuilder. append ( city== null ? "" : city. trim ( ) ) . append ( "/" ) ;
county= m. group ( "county" ) ;
stringBuilder. append ( county== null ? "" : county. trim ( ) ) ;
town= m. group ( "town" ) ;
stringBuilder. append ( town== null ? "" : town. trim ( ) ) ;
village= m. group ( "village" ) ;
stringBuilder. append ( village== null ? "" : village. trim ( ) ) ;
}
return stringBuilder. toString ( ) ;
}
public static void main ( String [ ] args) {
System . out. println ( addressResolution1 ( "河南省郑州市金水区" ) ) ;
}
}