메뉴 건너뛰기

조회 수 1164 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
import java.util.regex.Matcher;

import java.util.regex.Pattern;




....





while(true){

	String str = bf.readLine(); 

	if(str == null) break; 

				

  if(str.length() == 0) pw.println(); 

	

	str = this.removeTag(str);




	System.out.println(str);

}





....





public String removeTag(String str){		

Matcher mat;   




// script 처리 

Pattern script = Pattern.compile("<(no)?script[^>]*>.*?</(no)?script>",Pattern.DOTALL);  


mat = script.matcher(str);  

str = mat.replaceAll("");  


// style 처리

Pattern style = Pattern.compile("<style[^>]*>.*</style>",Pattern.DOTALL);  


mat = style.matcher(str);  

str = mat.replaceAll("");  

// tag 처리 


Pattern tag = Pattern.compile("<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>");  


mat = tag.matcher(str);  

str = mat.replaceAll("");  

// ntag 처리 


Pattern ntag = Pattern.compile("<\\w+\\s+[^<]*\\s*>");  


mat = ntag.matcher(str);  

str = mat.replaceAll("");  

// entity ref 처리


Pattern Eentity = Pattern.compile("&[^;]+;");  


mat = Eentity.matcher(str);  

str = mat.replaceAll("");

// whitespace 처리 


Pattern wspace = Pattern.compile("\\s\\s+");  

mat = wspace.matcher(str); 

str = mat.replaceAll(""); 	          




return str ;		

}

JAVA 에서도 정규표현식을 이용하여 패턴매칭이 가능하다. 


다음은 정규표현식을 사용하여, 숫자와 영문을 제거하는 소스코드이다. 

import java.util.regex.*;




....




	private String removeChar(String inp){

		// 띄어쓰기 제거

		String tmp = inp.replaceAll(" ", ""); 		

		// 숫자 제거 

		tmp = this.removeRex("[0-9]", tmp);  

		// 영문 제거 

		tmp = this.removeRex("[a-zA-Z]", tmp); 

	

		return tmp; 

	}

	

	// 패턴 제거

	private String removeRex(String rex, String inp){

		Pattern numP = Pattern.compile(rex); 

		Matcher mat = numP.matcher("");

		mat.reset(inp);

		inp = m.replaceAll("");

		return inp ; 

	}



매칭된 부분을 출력

Pattern script = Pattern.compile("\\[.*\\]$");  

mat = script.matcher(str);  

					

while(mat.find()){

	System.out.println(mat.group());

}


StackOverflowError

위의 소스를 돌리다 보면 위와같은 에러가 뜬다. 

그 이유는, 태그를 잘라낼 문자열의 길이가 너무 클때 생긴다고 한다. 해결방법은 없는듯하다. 

문자열을 잘라내서 분류를 하던 해야하겠다. 



List of Articles
번호 제목 날짜 조회 수
71 JQuery 자동완성 플러그인 JSDT설치 file 2016.09.19 8896
70 System.out.println(); 이클립스에서 자동화기능 사용 file 2016.09.19 5158
69 웹브라우저(크롬) 설정하여 웹을 실행해 보자 file 2016.09.19 7205
68 한글이 깨졌다! 인코딩(encoding)을 설정하자 file 2016.09.19 4715
67 이클립스를 화려하게 꾸며보자 file 2016.09.19 4457
66 Eclipse에서 import문 자동으로 작성하는 기능 file 2016.09.19 4811
65 Jadclipse 플러그인 설치 file 2016.09.19 3512
64 이클립스 플러그인 삭제방법 file 2016.09.19 4721
63 공통컴포넌트 생성하기 - 게시판 file 2016.09.19 5754
62 전자정부 프레임워크란? file 2016.09.19 8342
61 JAR 파일 2016.09.19 3713
60 자바용 날짜 계산 2016.09.19 4543
59 클래스 객체 생성과 사용 2016.09.19 3377
58 예외 만들기 2016.09.13 4961
57 예외 처리 기본 file 2016.09.13 3249
56 enum 2016.09.13 3404
55 for-each문 file 2016.09.13 3120
54 오토박싱 2016.09.13 3246
53 익명클래스 2016.09.13 3143
52 public static void main(String [] args) 2016.09.13 3143
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved