Notice
Recent Posts
Recent Comments
Link
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
03-29 01:54
관리 메뉴

zyint's blog

HTML 태그 제거 정규식 본문

예전글들

HTML 태그 제거 정규식

진트­ 2008. 10. 2. 13:10

HTML 태그 제거 정규식#

 

태그제거[1] #

  1. // 정규표현식으로 제거
     String.replaceAll("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>","");

 

한 줄로 표현하기[1] #

  1.  // 한줄로 할려면 아래 추가
     String.replaceAll("("\r|\n|&nbsp;","");

 

HTML 태그 제거 소스 [2] #

  1. public class HTMLCleaner
    {
  2.  public static void main(String[] args)
     {
      HTMLCleaner cleaner = new HTMLCleaner();
  3.   //System.out.println(cleaner.clean("<html><head><script>aaaa</script></head><body><div>aaa</div> <div> <script></script></div><img src=\"
  4.  }
  5.  private static interface Patterns
     {
      // javascript tags and everything in between
      public static final Pattern SCRIPTS = Pattern.compile(
        "<(no)?script[^>]*>.*?</(no)?script>",
        Pattern.DOTALL);
  6.   public static final Pattern STYLE = Pattern.compile(
        "<style[^>]*>.*</style>",
        Pattern.DOTALL);
      // HTML/XML tags
  7.   public static final Pattern TAGS = Pattern.compile("<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>");
  8.   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+");
     }
  9.  /**
      * Clean the HTML input.
      */
     public String clean(String s)
     {
      if (s == null)
      {
       return null;
      }
  10.   Matcher m;
  11.   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(" ");
  12.   return s;
     }
  13. }

 

참고자료#

(1)  a b  http://fairworld.tistory.com/138

(2) a http://okjsp.pe.kr/seq/111879

 

 

 

 

 

 

이 글은 스프링노트에서 작성되었습니다.

Comments