Java Blog From WeiChunHua

Java

常用链接

统计

develop

news

最新评论

JAVA正则表达式

public class Regular{

public static void main(String[]args){

//把字符串中的 "aaa"全部替换为"z" 打印zbzcz

System.out.println("aaabaaacaaa".replaceAll("a{3}","z"));

//把字符串的"aaa","aa"或者"a" 全部替换为"*" 打印*b*c*

System.out.println("aaabaaca".replaceAll("a{1,3},"\\*"));

//把字符串中的数字全部替换为"z" 打印zzzazzbzzcc

System.out.println("123a44b35cc".replaceAll("\ \d","z"));

//把字符串中的非数字全部替换为"0" 打印1234000435000

System.out.println("1234abc435def".replaceAll("\ \D","0"));

//把字符串中的"."全部替换为"\"打印abc\def\ghi\jk

System.out.println("abc.def.ghi.jk".replaceAll("\\.","\\\\"));

//把字符串中的"a.b"全部替换为"-","a.b"表示长度为3的字符串,以a开头以b结尾

//打印-hello-lining

System.out.println("axbhelloasblining".replaceAll("a.b","-"));

//把字符串中的所有词字符替换为"#"

//正则表达式"[a-zA-Z_0-9]等价于"\w"

//打印#.#.#.#.#.

System.out.println("a.b.c.1.2.".replace("\w","#"));

}



/**
     * filter all html element.
     * For example:<a href="www.sohu.com/test">hello!</a>
     * The filter result is :hello!
     * Notice:This method filter the text between "<" and ">"
     * @param element
     * @return
     */
     public static String getTxtWithoutHTMLElement (String element)
     {
//       String reg="<[^<|^>]+>";
//       return   element.replaceAll(reg,"");
        
         if(null==element||"".equals(element.trim()))
         {
             return element;
         }

         Pattern pattern=Pattern.compile("<[^<|^>]*>");
         Matcher matcher=pattern.matcher(element);
         StringBuffer txt=new StringBuffer();
         while(matcher.find())
         {
             String group=matcher.group();
             if(group.matches("<[\\s]*>"))
             {
                 matcher.appendReplacement(txt,group);    
             }
             else
             {
                 matcher.appendReplacement(txt,"");
             }
         }
         matcher.appendTail(txt);
         repaceEntities(txt,"&","&");
         repaceEntities(txt,"<","<");        
         repaceEntities(txt,">",">");
         repaceEntities(txt,""","\"");
         repaceEntities(txt," ","");
        
         return txt.toString();
     }



下面是测试用例:
public void testGetTxtWithoutHTMLElement ()
     {
        
         assertEquals("test",ExcelHssfView.getTxtWithoutHTMLElement("<a href='a/test'>test</a>"));
        
         assertEquals("test",ExcelHssfView.getTxtWithoutHTMLElement("<a href='a/test'>test"));
        
         assertEquals("test",ExcelHssfView.getTxtWithoutHTMLElement("<input type='text'>test</input>"));
        
         assertEquals("test",ExcelHssfView.getTxtWithoutHTMLElement("<p>test"));
        
         assertEquals("test",ExcelHssfView.getTxtWithoutHTMLElement("<table><tr><td>test</td></tr></table>"));
        
         assertEquals("te<st",ExcelHssfView.getTxtWithoutHTMLElement("<p>te<st"));
        
         assertEquals("te>st",ExcelHssfView.getTxtWithoutHTMLElement("<p>te>st"));
        
         assertEquals("tst",ExcelHssfView.getTxtWithoutHTMLElement("<p>t<e>st"));
        
         assertEquals("t<st",ExcelHssfView.getTxtWithoutHTMLElement("<p>t<<e>st"));
        
         assertEquals("<>test",ExcelHssfView.getTxtWithoutHTMLElement("<p><>test"));
        
         assertEquals("< >test",ExcelHssfView.getTxtWithoutHTMLElement("<p>< >test"));
        
         assertEquals("<<>test",ExcelHssfView.getTxtWithoutHTMLElement("<p><<>test"));
        
         assertEquals("test",ExcelHssfView.getTxtWithoutHTMLElement("<table><tr><td> test</td></tr></table>"));
        
     }

}

posted on 2008-06-30 10:58 sunny spring 阅读(153) 评论(0)  编辑  收藏 所属分类: javaee


只有注册用户登录后才能发表评论。


网站导航: