当前位置: 移动技术网 > IT编程>开发语言>正则 > Java正则表达式过滤出字母、数字和中文

Java正则表达式过滤出字母、数字和中文

2017年12月08日  | 移动技术网IT编程  | 我要评论
1、java中过滤出字母、数字和中文的正则表达式 (1)过滤出字母的正则表达式       [^(a-za-z)]

1、java中过滤出字母、数字和中文的正则表达式

(1)过滤出字母的正则表达式

      [^(a-za-z)]

(2) 过滤出 数字 的正则表达式

   [^(0-9)]

(3) 过滤出 中文 的正则表达式

       [^()]

(4) 过滤出字母、数字和中文的正则表达式

       [^(a-za-z0-9\\u4e00-\\u9fa5)]

2、实例源码

**
 * @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李四

ps:java正则表达式过滤汉字

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你好"));
 }
}

以上内容是关于java正则表达式过滤中文、字母、数字的全部叙述,希望大家喜欢。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网