Java正则表达式过滤出字母、数字和中⽂1、Java中过滤出字母、数字和中⽂的正则表达式
(1)过滤出字母的正则表达式
[^(A-Za-z)]
(2)过滤出数字的正则表达式
[^(0-9)]
(3)过滤出中⽂的正则表达式
[^(\\u4e00-\\u9fa5)]
(4)过滤出字母、数字和中⽂的正则表达式
[^(a-zA-Z0-9\\u4e00-\\u9fa5)]
2、实例源码
**
* @Title:FilterStr.java
* @u.dao
* @Description:Java中过滤数字、字母和中⽂
* @Author: 游海东
* @date: 2014年3⽉12⽇下午7:18:20
* @Version V1.2.3
*/
u.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 = placeAll("[^(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 = placeAll("[^(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 = placeAll("[^(\\u4e00-\\u9fa5)]", "");
return chin;
}
/**
*
* @Title : filter
* @Type : FilterStr
* @date : 2014年3⽉12⽇下午9:17:22
* @Description : 过滤出字母、数字和中⽂
* @param character
replaceall()* @return
*/
public static String filter(String character)
{
character = placeAll("[^(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李四ps:Java正则表达式过滤汉字
String str = "hello你好吗,我很好 thank you";
String reg = "[\u2E80-\u9FFF]";
Pattern pat = Patternpile(reg);
Matcher mat = pat.matcher(str);
String repickStr = placeAll("");
System.out.println("过滤中⽂后: "+repickStr);
Demo:
import Matcher;
import Pattern;
public class T {
/
**
* 过滤字母
* @param alphabet
* @return
*/
public static String filterAlphabet(String alphabet){
placeAll("[A-Za-z]", "");
}
/**
* 过滤数字
* @param digital
* @return
*/
public static String filterDigital(String digital){
placeAll("[0-9]", "");
}
/**
* 过滤汉字
* @param chin
* @return
*/
public static String filterChinese(String chin){
placeAll("[\\u4e00-\\u9fa5]", "");
}
/**
* 过滤字母、数字、汉字
* @param character
* @return
*/
public static String filterAll(String character){
placeAll("[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 = Patternpile(reg);
Matcher mat = pat.matcher(str);
String repickStr = placeAll("");
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你好"));
}
}
以上内容是关于java正则表达式过滤中⽂、字母、数字的全部叙述,希望⼤家喜欢。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。