반응형
HTML 태그 제거 정규식#
태그제거[1]↓ #
- // 정규표현식으로 제거
String.replaceAll("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>","");
한 줄로 표현하기[1]↓ #
- String.replaceAll("("\r|\n","");
주석 제거#
- Pattern.compile("<!--.*-->", Pattern.DOTALL)
.matcher(text)
.replaceAll("");
HTML 태그 제거 소스 [2]↓ #
- public class HTMLCleaner
{ - public static void main(String[] args)
{
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;
} - }
* 완전하지는 않다.
See also#
참고자료#
(1) a b http://fairworld.tistory.com/138
(2) a http://okjsp.pe.kr/seq/111879