public static void main(String[] args) {
Pattern p = Pattern.compile("([A-Za-z0-9\u4e00-\u9fa5]{1,}縣)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}市)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}區)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}鎮)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}鄉)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}路)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}街)"+
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}小段)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}段)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}巷)" +
"|([A-Za-z0-9\u4e00-\u9fa5]{1,}號)");
Matcher m = p.matcher("嘉义市嘉义市");
String shi = null;
String qu = null;
String lu = null;
String jie = null;
String xiaoduan = null;
String duan = null;
String xiang = null;
String hao = null;
while(m.find()){
String str1 = m.group();
System.out.println(str1);
if(str1.contains("縣")){//可能一個縣,也可能兩個縣
int count = countStr(str1,"縣");
if(count == 2){
shi = str1.substring(0,str1.indexOf("縣")+1);
if(countStr(str1,shi) == 2){
qu = shi;
}else{
qu = str1.replace(shi, "");
}
System.out.println("shi==="+shi);
System.out.println("qu==="+qu);
}else if(count == 1){
if(null != shi){
qu = str1;
System.out.println("qu==="+qu);
}else{
if(str1.contains("市")){
shi = str1.substring(0,str1.indexOf("市")+1);
qu = str1.replace(shi, "");
}else{
shi = str1;
System.out.println("shi==="+shi);
}
}
}
}else if(str1.contains("市")){//可能一個市,也可能兩個市
int count = countStr(str1,"市");
if(count == 2){
shi = str1.substring(0,str1.indexOf("市")+1);
if(countStr(str1,shi) == 2){
qu = shi;
}else{
qu = str1.replace(shi, "");
}
System.out.println("shi==="+shi);
System.out.println("qu==="+qu);
}else if(count == 1){
if(null != shi){
qu = str1;
System.out.println("qu==="+qu);
}else{
shi = str1;
System.out.println("shi==="+shi);
}
}
}else if(str1.contains("區")|str1.contains("鎮")|str1.contains("鄉")){
qu = str1;
System.out.println("qu==="+qu);
}else if(str1.contains("路")){
lu = str1;
}else if(str1.contains("街")){
jie = str1;
}else if(str1.contains("小段")){
xiaoduan = str1;
}else if(str1.contains("段")){
duan = str1;
}else if(str1.contains("巷")){
xiang = str1;
}else if(str1.contains("號")){
hao = str1;
}
}
}
Java通过正则表达式提取市区路段号。。。
最新推荐文章于 2024-08-30 10:03:41 发布