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

原文:http://blog.csdn.net/k21325/article/details/54090066

 

 

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

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

 

[html]  view plain  copy
 
  1. [^(A-Za-z)]  

 

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

 

[html]  view plain  copy
 
  1. [^(0-9)]  

 

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

 

[html]  view plain  copy
 
  1. [^(\\u4e00-\\u9fa5)]  

 

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

 

[html]  view plain  copy
 
  1. [^(a-zA-Z0-9\\u4e00-\\u9fa5)]  

 

2、实例源码

[java]  view plain  copy
 
  1. **  
  2.  * @Title:FilterStr.java  
  3.  * @Package:com.you.dao  
  4.  * @Description:Java中过滤数字、字母和中文  
  5.  * @Author: 游海东  
  6.  * @date: 2014年3月12日 下午7:18:20  
  7.  * @Version V1.2.3  
  8.  */  
  9. package com.you.dao;  
  10.    
  11. /** 
  12.  * @类名:FilterStr 
  13.  * @描述:正则表达式过滤数字、字母和中文 
  14.  * @Author:游海东 
  15.  * @date: 2014年3月12日 下午7:18:20 
  16.  */  
  17. public class FilterStr   
  18. {  
  19.  /** 
  20.  *  
  21.  * @Title : filterNumber 
  22.  * @Type : FilterStr 
  23.  * @date : 2014年3月12日 下午7:23:03 
  24.  * @Description : 过滤出数字 
  25.  * @param str 
  26.  * @return 
  27.  */  
  28.  public static String filterNumber(String number)  
  29.  {  
  30.  number = number.replaceAll("[^(0-9)]", "");  
  31.  return number;  
  32.  }  
  33.     
  34.  /** 
  35.  *  
  36.  * @Title : filterAlphabet 
  37.  * @Type : FilterStr 
  38.  * @date : 2014年3月12日 下午7:28:54 
  39.  * @Description : 过滤出字母 
  40.  * @param alph 
  41.  * @return 
  42.  */  
  43.  public static String filterAlphabet(String alph)  
  44.  {  
  45.  alph = alph.replaceAll("[^(A-Za-z)]", "");  
  46.  return alph;  
  47.  }  
  48.     
  49.  /** 
  50.  *  
  51.  * @Title : filterChinese 
  52.  * @Type : FilterStr 
  53.  * @date : 2014年3月12日 下午9:12:37 
  54.  * @Description : 过滤出中文 
  55.  * @param chin 
  56.  * @return 
  57.  */  
  58.  public static String filterChinese(String chin)  
  59.  {  
  60.  chin = chin.replaceAll("[^(\\u4e00-\\u9fa5)]", "");  
  61.  return chin;  
  62.  }  
  63.     
  64.  /** 
  65.  *  
  66.  * @Title : filter 
  67.  * @Type : FilterStr 
  68.  * @date : 2014年3月12日 下午9:17:22 
  69.  * @Description : 过滤出字母、数字和中文 
  70.  * @param character 
  71.  * @return 
  72.  */  
  73.  public static String filter(String character)  
  74.  {  
  75.  character = character.replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5)]", "");  
  76.  return character;  
  77.  }  
  78.     
  79.  /** 
  80.  * @Title : main 
  81.  * @Type : FilterStr 
  82.  * @date : 2014年3月12日 下午7:18:22 
  83.  * @Description :  
  84.  * @param args 
  85.  */  
  86.  public static void main(String[] args)   
  87.  {  
  88.  /** 
  89.   * 声明字符串you 
  90.   */  
  91.  String you = "^&^&^you123$%$%你好";  
  92.  /** 
  93.   * 调用过滤出数字的方法 
  94.   */  
  95.  you = filterNumber(you);  
  96.  /** 
  97.   * 打印结果 
  98.   */  
  99.  System.out.println("过滤出数字:" + you);  
  100.     
  101.  /** 
  102.   * 声明字符串hai 
  103.   */  
  104.  String hai = "¥%……4556ahihdjsadhj$%$%你好吗wewewe";  
  105.  /** 
  106.   * 调用过滤出字母的方法 
  107.   */  
  108.  hai = filterAlphabet(hai);  
  109.  /** 
  110.   * 打印结果 
  111.   */  
  112.  System.out.println("过滤出字母:" + hai);  
  113.     
  114.  /** 
  115.   * 声明字符串dong 
  116.   */  
  117.  String dong = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";  
  118.  /** 
  119.   * 调用过滤出中文的方法 
  120.   */  
  121.  dong = filterChinese(dong);  
  122.  /** 
  123.   * 打印结果 
  124.   */  
  125.  System.out.println("过滤出中文:" + dong);  
  126.     
  127.  /** 
  128.   * 声明字符串str 
  129.   */  
  130.  String str = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";  
  131.  /** 
  132.   * 调用过滤出字母、数字和中文的方法 
  133.   */  
  134.  str = filter(str);  
  135.  /** 
  136.   * 打印结果 
  137.   */  
  138.  System.out.println("过滤出字母、数字和中文:" + str);  
  139.     
  140.  }  
  141.    
  142. }  

3、实例运行结果

过滤出数字:123
过滤出字母:ahihdjsadhjwewewe
过滤出中文:张三李四
过滤出字母、数字和中文:张三34584yuojk李四

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

 

[java]  view plain  copy
 
  1. String str = "hello你好吗,我很好 thank you";   
  2. String reg = "[\u2E80-\u9FFF]";   
  3. Pattern pat = Pattern.compile(reg);   
  4. Matcher mat = pat.matcher(str);   
  5. String repickStr = mat.replaceAll("");   
  6. System.out.println("过滤中文后: "+repickStr);  

 

[java]  view plain  copy
 
  1. import java.util.regex.Matcher;  
  2. import java.util.regex.Pattern;  
  3. public class T {  
  4.  /** 
  5.  * 过滤字母 
  6.  * @param alphabet 
  7.  * @return 
  8.  */  
  9.  public static String filterAlphabet(String alphabet){  
  10.  return alphabet.replaceAll("[A-Za-z]", "");  
  11.  }  
  12.  /** 
  13.  * 过滤数字 
  14.  * @param digital 
  15.  * @return 
  16.  */  
  17.  public static String filterDigital(String digital){  
  18.  return digital.replaceAll("[0-9]", "");  
  19.  }  
  20.  /** 
  21.  * 过滤汉字 
  22.  * @param chin 
  23.  * @return 
  24.  */  
  25.  public static String filterChinese(String chin){  
  26.  return chin.replaceAll("[\\u4e00-\\u9fa5]", "");  
  27.  }  
  28.  /** 
  29.  * 过滤 字母、数字、汉字 
  30.  * @param character 
  31.  * @return 
  32.  */  
  33.  public static String filterAll(String character){  
  34.  return character.replaceAll("[a-zA-Z0-9\\u4e00-\\u9fa5]", "");  
  35.  }  
  36.  /** 
  37.  * @param args 
  38.  */  
  39.  public static void main(String[] args) {  
  40.  // TODO Auto-generated method stub  
  41.  String str = "hello你好吗,我很好 thank you";   
  42.  String reg = "[\u2E80-\u9FFF]";   
  43.  Pattern pat = Pattern.compile(reg);   
  44.  Matcher mat = pat.matcher(str);   
  45.  String repickStr = mat.replaceAll("");   
  46.  System.out.println("过滤中文后: "+repickStr);   
  47.  System.out.println("-----------------");  
  48.  System.out.println(filterAlphabet("123abc你好"));  
  49.  System.out.println(filterDigital("123abc你好"));  
  50.  System.out.println(filterChinese("123abc你好"));  
  51.  System.out.println(filterAll("123abc你好"));  
  52.  }  
  53. }  


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

 

转载于:https://www.cnblogs.com/shihaiming/p/6978517.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值