Java正則表達式過濾出字母、數字和中文


原文:http://blog.csdn.net/k21325/article/details/54090066

 

 

1、Java中過濾出字母、數字和中文的正則表達式

(1)過濾出字母的正則表達式

 

[html]  view plain  copy
 
  1. [^(A-Za-z)]  

 

(2) 過濾出 數字 的正則表達式

 

[html]  view plain  copy
 
  1. [^(0-9)]  

 

(3) 過濾出 中文 的正則表達式

 

[html]  view plain  copy
 
  1. [^(\\u4e00-\\u9fa5)]  

 

(4) 過濾出字母、數字和中文的正則表達式

 

[html]  view plain  copy
 
  1. [^(a-zA-Z0-9\\u4e00-\\u9fa5)]  

 

2、實例源碼

[java]  view plain  copy
 
  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、實例運行結果

過濾出數字:123
過濾出字母:ahihdjsadhjwewewe
過濾出中文:張三李四
過濾出字母、數字和中文:張三34584yuojk李四

ps:Java正則表達式過濾漢字

 

[java]  view plain  copy
 
  1. String str = "hello你好嗎,我很好 thank you";   
  2. String reg = "[\u2E80-\u9FFF]";   
  3. Pattern pat = Pattern.compile(reg);   
  4. Matcher mat = pat.matcher(str);   
  5. String repickStr = mat.replaceAll("");   
  6. System.out.println("過濾中文后: "+repickStr);  

 

[java]  view plain  copy
 
  1. import java.util.regex.Matcher;  
  2. import java.util.regex.Pattern;  
  3. public class T {  
  4.  /** 
  5.  * 過濾字母 
  6.  * @param alphabet 
  7.  * @return 
  8.  */  
  9.  public static String filterAlphabet(String alphabet){  
  10.  return alphabet.replaceAll("[A-Za-z]", "");  
  11.  }  
  12.  /** 
  13.  * 過濾數字 
  14.  * @param digital 
  15.  * @return 
  16.  */  
  17.  public static String filterDigital(String digital){  
  18.  return digital.replaceAll("[0-9]", "");  
  19.  }  
  20.  /** 
  21.  * 過濾漢字 
  22.  * @param chin 
  23.  * @return 
  24.  */  
  25.  public static String filterChinese(String chin){  
  26.  return chin.replaceAll("[\\u4e00-\\u9fa5]", "");  
  27.  }  
  28.  /** 
  29.  * 過濾 字母、數字、漢字 
  30.  * @param character 
  31.  * @return 
  32.  */  
  33.  public static String filterAll(String character){  
  34.  return character.replaceAll("[a-zA-Z0-9\\u4e00-\\u9fa5]", "");  
  35.  }  
  36.  /** 
  37.  * @param args 
  38.  */  
  39.  public static void main(String[] args) {  
  40.  // TODO Auto-generated method stub  
  41.  String str = "hello你好嗎,我很好 thank you";   
  42.  String reg = "[\u2E80-\u9FFF]";   
  43.  Pattern pat = Pattern.compile(reg);   
  44.  Matcher mat = pat.matcher(str);   
  45.  String repickStr = mat.replaceAll("");   
  46.  System.out.println("過濾中文后: "+repickStr);   
  47.  System.out.println("-----------------");  
  48.  System.out.println(filterAlphabet("123abc你好"));  
  49.  System.out.println(filterDigital("123abc你好"));  
  50.  System.out.println(filterChinese("123abc你好"));  
  51.  System.out.println(filterAll("123abc你好"));  
  52.  }  
  53. }  


以上內容是關於java正則表達式過濾中文、字母、數字的全部敘述,希望大家喜歡。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM