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

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

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

     [^(A-Za-z)]

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

  [^(0-9)]

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

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

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

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

2、实例源码


/**
 * @Title:FilterStr.java
 * @Package:com.you.dao
 * @Description:Java中过滤数字、字母和中文
 * @Author: 游海东
 * @date: 2014年3月12日 下午7:18:20
 * @Version V1.2.3
 */
package com.you.dao;

/**
 * @类名:FilterStr
 * @描述:正则表达式过滤数字、字母和中文
 * @Author:游海东
 * @date: 2014年3月12日 下午7:18:20
 */
public class FilterStr 
{
  /**
   * 
   * @Title : filterNumber
   * @Type : FilterStr
   * @date : 2014年3月12日 下午7:23:03
   * @Description : 过滤出数字
   * @param str
   * @return
   */
  public static String filterNumber(String number)
  {
    number = number.replaceAll("[^(0-9)]", "");
    return number;
  }
  
  /**
   * 
   * @Title : filterAlphabet
   * @Type : FilterStr
   * @date : 2014年3月12日 下午7:28:54
   * @Description : 过滤出字母
   * @param alph
   * @return
   */
  public static String filterAlphabet(String alph)
  {
    alph = alph.replaceAll("[^(A-Za-z)]", "");
    return alph;
  }
  
  /**
   * 
   * @Title : filterChinese
   * @Type : FilterStr
   * @date : 2014年3月12日 下午9:12:37
   * @Description : 过滤出中文
   * @param chin
   * @return
   */
  public static String filterChinese(String chin)
  {
    chin = chin.replaceAll("[^(\\u4e00-\\u9fa5)]", "");
    return chin;
  }
  
  /**
   * 
   * @Title : filter
   * @Type : FilterStr
   * @date : 2014年3月12日 下午9:17:22
   * @Description : 过滤出字母、数字和中文
   * @param character
   * @return
   */
  public static String filter(String character)
  {
    character = character.replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5)]", "");
    return character;
  }
  
  /**
   * @Title : main
   * @Type : FilterStr
   * @date : 2014年3月12日 下午7:18:22
   * @Description : 
   * @param args
   */
  public static void main(String[] args) 
  {
    /**
     * 声明字符串you
     */
    String you = "^&^&^you123$%$%你好";
    /**
     * 调用过滤出数字的方法
     */
    you = filterNumber(you);
    /**
     * 打印结果
     */
    System.out.println("过滤出数字:" + you);
    
    /**
     * 声明字符串hai
     */
    String hai = "¥%……4556ahihdjsadhj$%$%你好吗wewewe";
    /**
     * 调用过滤出字母的方法
     */
    hai = filterAlphabet(hai);
    /**
     * 打印结果
     */
    System.out.println("过滤出字母:" + hai);
    
    /**
     * 声明字符串dong
     */
    String dong = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";
    /**
     * 调用过滤出中文的方法
     */
    dong = filterChinese(dong);
    /**
     * 打印结果
     */
    System.out.println("过滤出中文:" + dong);
    
    /**
     * 声明字符串str
     */
    String str = "$%$%$张三34584yuojk李四@#¥#%%¥……%&";
    /**
     * 调用过滤出字母、数字和中文的方法
     */
    str = filter(str);
    /**
     * 打印结果
     */
    System.out.println("过滤出字母、数字和中文:" + str);
    
  }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值