필요해서 하나 만들어본것.

ㅇㅇ


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class ParseAddress {
 
    public static void main(String[] args) {
        
        parseAddr("충남 영등포시 따따따구 듀라라라");
 
    }
 
    
public static String[] parseAddr(String sAddr) {
        
        String[] tmpArr;
        String[] rslt = new String[2];
        
        rslt[0= "";
        rslt[1= "";
        
        HashMap<String, Integer> hdStr = new HashMap<String, Integer>();
 
        String rgxPtn0 = "(^[^ ]+)\\s*(.*)$";
        String rgxPtn1 = "(^[^ ]+ [^ ]+구)\\s*(.*)$";
        String rgxPtn2 = "(^[^ ]+ [^ ]+군)\\s*(.*)$";        
        String rgxPtn31 = "(^[^ ]+ [^ ]+시 [^ ]+구)\\s*(.*)$";
        String rgxPtn32= "(^[^ ]+ [^ ]+시)\\s*(.*)$";        
        String rgxPtn33= "(^[^ ]+ [^ ]+군)\\s*(.*)$";
        
        Pattern ptn1 = Pattern.compile(rgxPtn1);
        Pattern ptn2 = Pattern.compile(rgxPtn2);
        Pattern ptn31 = Pattern.compile(rgxPtn31);
        Pattern ptn32 = Pattern.compile(rgxPtn32);
        Pattern ptn33 = Pattern.compile(rgxPtn33);
        
        hdStr.put("서울",1);
        hdStr.put("부산",2);
        hdStr.put("대구",2);
        hdStr.put("인천",2);
        hdStr.put("광주",1);
        hdStr.put("대전",1);
        hdStr.put("울산",2);
        hdStr.put("세종",0);
        hdStr.put("경기",3);
        hdStr.put("강원",3);
        hdStr.put("충청",3);
        hdStr.put("충남",3);
        hdStr.put("충북",3);
        hdStr.put("전라",3);
        hdStr.put("전남",3);
        hdStr.put("전북",3);
        hdStr.put("경상",3);
        hdStr.put("경남",3);
        hdStr.put("경북",3);
        hdStr.put("제주",3);
        
        if(sAddr == null) {
            return null;
        }
        
        sAddr = ltrim(sAddr);
        sAddr = rtrim(sAddr);
        
        if("".equals(sAddr)) {
            return null;
        }
 
        Matcher m;
        
        for(Map.Entry<String, Integer> tmp : hdStr.entrySet()) {
            
            if(sAddr.startsWith(tmp.getKey())) {
                switch(tmp.getValue()) {
                case 0 :
                    // 시
                    tmpArr = sAddr.replaceAll(rgxPtn0, "$1||$2").split("\\|\\|");
                    rslt[0= tmpArr[0];
                    
                    if(tmpArr.length > 1) { 
                        rslt[1= tmpArr[1];
                    }
                    
                    break;
                    
                case 1 :
                    // 구
                    tmpArr = sAddr.replaceAll(rgxPtn1, "$1||$2").split("\\|\\|");
                    rslt[0= tmpArr[0];
                    
                    if(tmpArr.length > 1) {
                        rslt[1= tmpArr[1];
                    }
                    break;
                    
                case 2 :
                    // 구
                    m = ptn1.matcher(sAddr);
                    
                    if(m.find()) {
                        rslt[0= m.group(1);
                        rslt[1= m.group(2);
                        break;
                    } 
                    
                    // 군                    
                    m = ptn2.matcher(sAddr);
 
                    if(m.find()) { 
                        rslt[0= m.group(1);
                        rslt[1= m.group(2);
                        break;
                    
                    }
                    
                    break;
                case 3 :
                    // 시 구
                    m = ptn31.matcher(sAddr);
                    
                    if(m.find()) {
                        rslt[0= m.group(1);
                        rslt[1= m.group(2);
                        break;
                    }
                    
                    // 시
                    m = ptn32.matcher(sAddr);
                    
                    if(m.find()) {
                        rslt[0= m.group(1);
                        rslt[1= m.group(2);
                        break;
                    }         
                    
                    // 군
                    m = ptn33.matcher(sAddr);
                    
                    if(m.find()) {
                        rslt[0= m.group(1);
                        rslt[1= m.group(2);
                        break;
                    }                     
                    break;
                }
            }
        }
        
        System.out.println("시군구 : " + rslt[0]);
        System.out.println("도로명 : " + rslt[1]);
        
        return rslt;
    }
    
    private final static Pattern LTRIM = Pattern.compile("^\\s+");
    private final static Pattern RTRIM = Pattern.compile("\\s+$");
 
    public static String ltrim(String s) {
        return LTRIM.matcher(s).replaceAll("");
    }
    
    public static String rtrim(String s) {
        return RTRIM.matcher(s).replaceAll("");
    }
}
 
 
cs