---
title: "过滤特殊字符及表情符"
author: "喧嚣求静"
date: "2014-04-01T14:07:46Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2014-11-08T10:15:38Z"
id: 2039455
url: "https://913.iteye.com/blog/2039455"
---

```java

//这个我从逆向考虑，找出常用合法的字符，其他为非法，过滤即可
public  String stringFilter(String str) throws PatternSyntaxException {
		// 只允许字母和数字和中文//[\\pp‘’“”
		string regex = &quot;^[A-Za-z\\d\\u4E00-\\u9FA5\\p{P}‘’“”]+$&quot;;
		Pattern p = Pattern.compile(regEx);
		StringBuilder sb = new StringBuilder(str);

		for (int len = str.length(), i = len - 1; i &gt;= 0; --i) {

			if (!p.matches(regEx, String.valueOf(str.charAt(i)))) {
				Toast.makeText(
						contex,
						contex.getResources().getString(R.string.emoji),
						Toast.LENGTH_SHORT);
				sb.deleteCharAt(i);
			}
		}

		return sb.toString();
	}

```


[LOL视频](http://www.loldv.com)