---
title: "过滤特殊字符"
author: "mywebcode"
date: "2013-12-04T09:51:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2014-04-30T09:09:05Z"
id: 2057838
url: "https://mywebcode.iteye.com/blog/2057838"
---

最近写的一个过滤特殊字符的方法

```
 // 过滤特殊字符  
    public 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();
      }
```