메뉴 건너뛰기

조회 수 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
번호 제목 날짜 조회 수
31 JDK 9에서 eclipse 실행이 안될때 2018.06.05 1596
30 java에서 이전 URL 알아내기 2021.03.25 690
29 JAVA/JSP SQL Injection 해킹 방지를 위한 코딩 2019.01.10 846
28 JAVA 현재 시간 구하기 file 2018.07.09 1266
» JAVA 정규표현식을 이용한 패턴매칭(HTML 제거) 2018.07.09 1164
26 JAVA 인코딩을 변경하여 파일 출력하기 (EUC_KR) 2018.07.09 1105
25 JAVA 이클립스 인코딩 변경하기 file 2018.07.09 1590
24 JAVA 두개의 문서 파일 비교하기 2018.07.09 2388
23 JAVA TreeMap 인덱스 값 가져오기 및 Collections.sort 사용하기 2018.07.09 1487
22 JAVA public, private, protected 정리 2018.07.09 1001
21 JAVA JDK 제거하기 file 2018.07.09 1409
20 JAVA HashMap의 Key값 출력하기 2018.07.09 1079
19 JAVA CentOS JDK 설치 및 환경변수 설정 file 2018.07.09 1821
18 Java : JSOUP 를 이용, html에서 소스, 링크경로 추출후 절대 경로로 바꾸기 2019.01.08 1166
17 JAR 파일 2016.09.19 3713
16 Jadclipse 플러그인 설치 file 2016.09.19 3512
15 iBATIS 동적으로 맵핑하기 2016.12.09 3949
14 HashMap 사용하기 file 2021.03.31 134
13 Gmail 메일 서버를 이용해서 메일 보내기 file 2020.06.29 256
12 GET 파라메타 쿼리문자열 컨트롤 : getQueryString 2016.12.09 4705
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved