[^(A-Za-z)]
[^(0-9)]
[^(\\\\u4e00-\\\\u9fa5)]
[^(a-zA-Z0-9\\\\u4e00-\\\\u9fa5)]
2、实例源码
?
|
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
**
* @Title:FilterStr.java
* @Package:com.you.dao
* @Description:Java中过滤数字、字母和中文
* @Author: 游海东
* @date: 2014年3月12日 下午7:18:20
* @Version V1.2.3
*/
package com.you.dao;
/**
* @类名:FilterStr
* @描述:正则表达式过滤数字、字母和中文
* @Author:游海东
* @date: 2014年3月12日 下午7:18:20
*/
public class FilterStr
{
/**
*
* @Title : filterNumber
* @Type : FilterStr
* @date : 2014年3月12日 下午7:23:03
* @Description : 过滤出数字
* @param str
* @return
*/
public static String filterNumber(String number)
{
number = number.replaceAll("[^(0-9)]", "");
return number;
}
/**
*
* @Title : filterAlphabet
* @Type : FilterStr
* @date : 2014年3月12日 下午7:28:54
* @Description : 过滤出字母
* @param alph
* @return
*/
public static String filterAlphabet(String alph)
{
alph = alph.replaceAll("[^(A-Za-z)]", "");
return alph;
}
/**
*
* @Title : filterChinese
* @Type : FilterStr
* @date : 2014年3月12日 下午9:12:37
* @Description : 过滤出中文
* @param chin
* @return
*/
public static String filterChinese(String chin)
{
chin = chin.replaceAll("[^(\\\\u4e00-\\\\u9fa5)]", "");
return chin;
}
/**
*
* @Title : filter
* @Type : FilterStr
* @date : 2014年3月12日 下午9:17:22
* @Description : 过滤出字母、数字和中文
* @param character
* @return
*/
public static String filter(String character)
{
character = character.replaceAll("[^(a-zA-Z0-9\\\\u4e00-\\\\u9fa5)]", "");
return character;
}
/**
* @Title : main
* @Type : FilterStr
* @date : 2014年3月12日 下午7:18:22
* @Description :
* @param args
*/
public static void main(String[] args)
{
/**
* 声明字符串you
*/
String you = "^&^&^you123$%$%你好";
/**
* 调用过滤出数字的方法
*/
you = filterNumber(you);
/**
* 打印结果
*/
System.out.println("过滤出数字:" + you);
/**
* 声明字符串hai
*/
String hai = "¥%……4556ahihdjsadhj$%$%你好吗wewewe";
/**
* 调用过滤出字母的方法
*/
hai = filterAlphabet(hai);
/**
* 打印结果
*/
System.out.println("过滤出字母:" + hai);
/**
* 声明字符串dong
*/
String dong = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";
/**
* 调用过滤出中文的方法
*/
dong = filterChinese(dong);
/**
* 打印结果
*/
System.out.println("过滤出中文:" + dong);
/**
* 声明字符串str
*/
String str = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";
/**
* 调用过滤出字母、数字和中文的方法
*/
str = filter(str);
/**
* 打印结果
*/
System.out.println("过滤出字母、数字和中文:" + str);
}
}
|
3、实例运行结果
过滤出数字:123
过滤出字母:ahihdjsadhjwewewe
过滤出中文:张三李四
过滤出字母、数字和中文:张三34584yuojk李四
?
|
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
String str = "hello你好吗,我很好 thank you";
String reg = "[\\u2E80-\\u9FFF]";
Pattern pat = Pattern.compile(reg);
Matcher mat = pat.matcher(str);
String repickStr = mat.replaceAll("");
System.out.println("过滤中文后: "+repickStr);
Demo:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class T {
/**
* 过滤字母
* @param alphabet
* @return
*/
public static String filterAlphabet(String alphabet){
return alphabet.replaceAll("[A-Za-z]", "");
}
/**
* 过滤数字
* @param digital
* @return
*/
public static String filterDigital(String digital){
return digital.replaceAll("[0-9]", "");
}
/**
* 过滤汉字
* @param chin
* @return
*/
public static String filterChinese(String chin){
return chin.replaceAll("[\\\\u4e00-\\\\u9fa5]", "");
}
/**
* 过滤 字母、数字、汉字
* @param character
* @return
*/
public static String filterAll(String character){
return character.replaceAll("[a-zA-Z0-9\\\\u4e00-\\\\u9fa5]", "");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "hello你好吗,我很好 thank you";
String reg = "[\\u2E80-\\u9FFF]";
Pattern pat = Pattern.compile(reg);
Matcher mat = pat.matcher(str);
String repickStr = mat.replaceAll("");
System.out.println("过滤中文后: "+repickStr);
System.out.println("-----------------");
System.out.println(filterAlphabet("123abc你好"));
System.out.println(filterDigital("123abc你好"));
System.out.println(filterChinese("123abc你好"));
System.out.println(filterAll("123abc你好"));
}
}
|
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
在ASP.NET 2.0中操作数据之三十一:使用DataList来一行显示多条记录
2025-05-29 77 -
springboot 高版本后继续使用log4j的完美解决方法
2025-05-27 40 -
2025-06-04 54
-
2025-06-04 88
-
2025-05-29 13
热门评论

