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

本文介绍了一种使用Java正则表达式从字符串中过滤出字母、数字和中文字符的方法。通过提供具体的正则表达式和示例代码,读者可以学习如何在Java中实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. /**
  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. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值