Pattern 과 Matcher를 이용해서 html 파싱을 하고있습니다.

" String strMatches1 = responseBody.substring(matches.start(), matches.end()); " 이부분을 단독으로 쓰면
아무 오류 없이 그냥 잘 실행 되는데

첨부로 되어진 소스와 같이 중복해서 두군데다가 쓰면 Exception을 던져 버립니다.

---------------------------------------------------< 첨부된 소스 >-----------------------------------------------
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL);          

ResponseHandler<String> responseHandler = new BasicResponseHandler();

try{
         String responseBody = httpclient.execute(httpget, responseHandler);        

         Pattern titlePattern = Pattern.compile("a>[^<>]*<");
         Matcher matches = titlePattern.matcher(responseBody);
         
         Pattern gragePattern = Pattern.compile("b>[^<>]*<");
         Matcher matches2 = gragePattern.matcher(responseBody);

         while ( matches.find() ) {
           String strMatches1 = responseBody.substring(matches.start(), matches.end());
          
           String strMatches2 = responseBody.substring(matches2.start() , matches2.end());
         }

}catch(Exception e ){ }
,...

왜 그러는지 아시는분 답변좀 주세요 ㅠ.ㅠ

위와 같이 할려는 이유는
원래는  이런 식으로 썻는데 쓸데없이 반복문 두번 쓰는 것같애서  이렇게 사용하면 Exception이 발생하지않습니다.

         Pattern titlePattern = Pattern.compile("a>[^<>]*<");
         Matcher matches = titlePattern.matcher(responseBody);

         while ( matches.find() ) {
           String strMatches1 = responseBody.substring(matches.start(), matches.end());
        }

         Pattern gragePattern = Pattern.compile("b>[^<>]*<");
         Matcher matches2 = gragePattern.matcher(responseBody);

         while ( matches2.find() ) {
           String strMatches2 = responseBody.substring(matches2.start() , matches2.end());
         }

-------------<html문서 >----------------
<html>
<a>이름</a>하하하
<b>주소</b>대구광역시
<a>이름</a>하하하2
<b>주소</b>대구광역시2
<a>이름</a>하하하3
<b>주소</b>대구광역시3
...

이런식으로 셋트로 테그로 싸여 있어서 반복문 한번 쓸려고 이런 식으로 했습니다.

읽어주셔서 감사합니다.
profile