AJPFX总结关于Java中过滤出字母、数字和中文的正则表达式

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

猜你喜欢

转载自www.cnblogs.com/AJPFX/p/10852085.html