主题:Java过滤特殊字符的正则表达式

主题:Java过滤特殊字符的正则表达式

// 过滤特殊字符
	public   static   String StringFilter(String   str)   throws   PatternSyntaxException   {   
                // 只允许字母和数字		
                // String   regEx  =  "[^a-zA-Z0-9]";                   
                   // 清除掉所有特殊字符
		  String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
		  Pattern   p   =   Pattern.compile(regEx);   
		  Matcher   m   =   p.matcher(str);   
		  return   m.replaceAll("").trim();   
		  }   
	@Test	    
	public    void   testStringFilter()   throws   PatternSyntaxException   {   
		  String   str   =   "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*).{}+.|.)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符¥……{}【】。,;’“‘”?";   
		  System.out.println(str);   
		  System.out.println(StringFilter(str));   
		  }  

猜你喜欢

转载自dy-f.iteye.com/blog/2330078