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.    * @Title : filterAlphabet

  35.    * @Type : FilterStr

  36.    * @date : 2014年3月12日 下午7:28:54

  37.    * @Description : 过滤出字母

  38.    * @param alph

  39.    * @return

  40.    */

  41.   public static String filterAlphabet(String alph)

  42.   {

  43.     alph = alph.replaceAll("[^(A-Za-z)]", "");

  44.     return alph;

  45.   }

  46.   /**

  47.    * 

  48.    * @Title : filterChinese

  49.    * @Type : FilterStr

  50.    * @date : 2014年3月12日 下午9:12:37

  51.    * @Description : 过滤出中文

  52.    * @param chin

  53.    * @return

  54.    */

  55.   public static String filterChinese(String chin)

  56.   {

  57.     chin = chin.replaceAll("[^(\\u4e00-\\u9fa5)]", "");

  58.     return chin;

  59.   }

  60.   /**

  61.    * 

  62.    * @Title : filter

  63.    * @Type : FilterStr

  64.    * @date : 2014年3月12日 下午9:17:22

  65.    * @Description : 过滤出字母、数字和中文

  66.    * @param character

  67.    * @return

  68.    */

  69.   public static String filter(String character)

  70.   {

  71.     character = character.replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5)]", "");

  72.     return character;

  73.   }

  74.   /**

  75.    * @Title : main

  76.    * @Type : FilterStr

  77.    * @date : 2014年3月12日 下午7:18:22

  78.    * @Description : 

  79.    * @param args

  80.    */

  81.   public static void main(String[] args) 

  82.   {

  83.     /**

  84.      * 声明字符串you

  85.      */

  86.     String you = "^&^&^you123$%$%你好";

  87.     /**

  88.      * 调用过滤出数字的方法

  89.      */

  90.     you = filterNumber(you);

  91.     /**

  92.      * 打印结果

  93.      */

  94.     System.out.println("过滤出数字:" + you);

  95.     /**

  96.      * 声明字符串hai

  97.      */

  98.     String hai = "¥%……4556ahihdjsadhj$%$%你好吗wewewe";

  99.     /**

  100.      * 调用过滤出字母的方法

  101.      */

  102.     hai = filterAlphabet(hai);

  103.     /**

  104.      * 打印结果

  105.      */

  106.     System.out.println("过滤出字母:" + hai);

  107.     /**

  108.      * 声明字符串dong

  109.      */

  110.     String dong = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";

  111.     /**

  112.      * 调用过滤出中文的方法

  113.      */

  114.     dong = filterChinese(dong);

  115.     /**

  116.      * 打印结果

  117.      */

  118.     System.out.println("过滤出中文:" + dong);

  119.     /**

  120.      * 声明字符串str

  121.      */

  122.     String str = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";

  123.     /**

  124.      * 调用过滤出字母、数字和中文的方法

  125.      */

  126.     str = filter(str);

  127.     /**

  128.      * 打印结果

  129.      */

  130.     System.out.println("过滤出字母、数字和中文:" + str);

  131.   }

  132. }

评论

暂无评论