메뉴 건너뛰기

조회 수 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
번호 제목 날짜 조회 수
51 로그인 컴포넌트 설치시 뷰 생성 에러 해결방법 (ORA-01031: 권한이 불충분합니다) file 2016.08.29 3874
50 개인정보 마스킹처리 (휴대폰번호, 이메일) 2018.06.26 3942
49 iBATIS 동적으로 맵핑하기 2016.12.09 3949
48 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ]11. 이클립스 프로젝트 생성 file 2016.08.18 3972
47 변수의 종류 2016.09.13 3978
46 프로젝트 & 아파치 &톰켓 연동 ]1. 폴더 만들기 file 2016.08.18 3983
45 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ]9. 이클립스 압타나 플러그인 설치 file 2016.08.18 4012
44 jquery 스크롤(scroll) 따라다니는 배너 레이어 / 위로 버튼 / 화면 상단으로 이동 / scroll layer 이벤트 file 2017.07.05 4057
43 Database Connections 생성하기 (오라클) file 2016.08.29 4072
42 war로 묶지 않아도 컴파일된 소스 위치 확인하기 file 2016.08.29 4116
41 Spring Boot 프로젝트 생성 file 2016.09.02 4160
40 프로젝트 & 아파치 &톰켓 연동 ]2. 자바 설치 file 2016.08.18 4170
39 eclipse 콘솔(로그)에 디버그(Debug) 모드에서 실행된 쿼리문을 보여주자. - 전자정부프레임워크 오라클 file 2016.08.29 4172
38 자바 다양한 형변환. 그리고 아스키 코드 String char int : JAVA 2016.12.09 4188
37 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서04.jdbc 드라이버 설치 file 2016.08.18 4209
36 전자정부 표준프레임워크 설치하기 file 2016.08.29 4250
35 배치관리 컴포넌트 생성 후 에러 날 때 해결방법 file 2016.08.29 4267
34 My-SQL 을 이용한 JDBC file 2016.09.21 4282
33 자바 JXL 엑셀파일을 읽어 배열리턴 : JAVA EXCEL ArrayList 2016.12.09 4389
32 이클립스를 화려하게 꾸며보자 file 2016.09.19 4457
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved