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

来源:互联网 发布:linux 解压到根目录 编辑:程序博客网 时间:2024/05/16 12:33

(转载)http://blog.csdn.net/you23hai45/article/details/21117315?utm_source=tuicool&utm_medium=referral


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

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

     [^(A-Za-z)]


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

  [^(0-9)]


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

       [^(\\u4e00-\\u9fa5)]


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

       [^(a-zA-Z0-9\\u4e00-\\u9fa5)]


2、实例源码

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  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、实例运行结果

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 过滤出数字:123  
  2. 过滤出字母:ahihdjsadhjwewewe  
  3. 过滤出中文:张三李四  
  4. 过滤出字母、数字和中文:张三34584yuojk李四  


0 0