메뉴 건너뛰기

조회 수 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 enum 2016.09.13 3404
70 문자 기반 스트림 2016.09.13 3440
69 쓰레드의 우선순위 2016.09.13 3503
68 파일I/O 개요 file 2016.09.13 3507
67 Jadclipse 플러그인 설치 file 2016.09.19 3512
66 인터페이스와 다형성 2016.09.13 3535
65 File 클래스 file 2016.09.13 3603
64 원하는 패턴의 날짜 구하기 : JAVA 2016.12.09 3608
63 새로 만든 모듈의 iBatis 쿼리를 새로운 xml 파일에 만들고 싶다면 이렇게 하자. file 2016.08.29 3651
62 set get 파라미터 2016.08.18 3658
61 컬렉션 프레임워크 file 2016.09.13 3705
60 JAR 파일 2016.09.19 3713
59 BigDecimal타입의 사칙연산 2016.12.22 3713
58 클래스 메서드와 인스턴스 메서드 2016.09.13 3720
57 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동]3. 이클립스 설치 file 2016.08.18 3756
56 바이트 기반의 스트림 file 2016.09.13 3763
55 static멤버와 관련된 예제 2016.09.21 3768
54 새로 만든 모듈의 iBatis 쿼리를 새로운 xml 파일에 만들고 싶다면 이렇게 하자. file 2016.08.29 3773
53 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서07.commons-collection 설치 file 2016.08.18 3845
52 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3848
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved