-
[정규표현식] html 태그에서 stript와 style 태그 내부에 있는 것들 제거Legacy/JAVA 2015. 10. 25. 21:40728x90
출처 : http://okky.kr/article/111879
public class HTMLCleaner {
/**
* @author Shrek
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HTMLCleaner cleaner = new HTMLCleaner();
//System.out.println(cleaner.clean("<html><head><script>aaaa</script></head><body><div>aaa</div> <div> <script></script></div><img src=\"http://tong.nate.com\" values=\">\"> 이건 어떻게 될까요 <!-- zmzm --></body></html> "));
}
private static interface Patterns {
// javascript tags and everything in between
public static final Pattern SCRIPTS = Pattern.compile("<(no)?script[^>]*>.*?</(no)?script>", Pattern.DOTALL);
public static final Pattern STYLE = Pattern.compile("<style[^>]*>.*</style>", Pattern.DOTALL);
// HTML/XML tags
public static final Pattern TAGS = Pattern.compile("<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>");
public static final Pattern nTAGS = Pattern.compile("<\\w+\\s+[^<]*\\s*>");
// entity references
public static final Pattern ENTITY_REFS = Pattern.compile("&[^;]+;");
// repeated whitespace
public static final Pattern WHITESPACE = Pattern.compile("\\s\\s+");
}
/**
* Clean the HTML input.
*/
public String clean(String s) {
if (s == null) {
return null;
}
Matcher m;
m = Patterns.SCRIPTS.matcher(s);
s = m.replaceAll("");
m = Patterns.STYLE.matcher(s);
s = m.replaceAll("");
m = Patterns.TAGS.matcher(s);
s = m.replaceAll("");
m = Patterns.ENTITY_REFS.matcher(s);
s = m.replaceAll("");
m = Patterns.WHITESPACE.matcher(s);
s = m.replaceAll(" ");
return s;
}
}'Legacy > JAVA' 카테고리의 다른 글
Connect Error: Access denied for user 'root'@'localhost' 에러 해결방법 (0) 2019.03.10 초보 개발자를 위한 스택트레이스 읽는 법 - 오키 fender님 글 (0) 2016.07.23 [JSP] <dispatcher>엘리먼트를 이용하여 경우에 맞는 필터 호출하는 방법 (0) 2015.10.13 [Java] String 과 String Buffer, String Builder의 차이점 (0) 2015.10.13 [JSP] 쿠키와 세션에 사용되는 메소드 (0) 2015.09.15 [JSP] RequestDispatcher.forward 메소드와 sendRedirect 메소드의 차이점 (4) 2015.09.12 [JSP] JSP 페이지는? (0) 2015.09.07 [JSP] JSP(Java Server Pages)와 자바 서블릿(Java Servlet) (0) 2015.08.26 Java 의 Vector 와 ArrayList , Linked List 의 차이점 (0) 2015.05.04 JSmooth 사용법 (0) 2014.12.09