一、Java中過濾出字母、數字和中文的正則表達式java
(1)過濾出字母的正則表達式正則表達式
[^(A-Za-z)]spa
(2) 過濾出 數字 的正則表達式code
[^(0-9)]blog
(3) 過濾出 中文 的正則表達式 ip
[^(\\u4e00-\\u9fa5)]字符串
(4) 過濾出字母、數字和中文的正則表達式源碼
[^(a-zA-Z0-9\\u4e00-\\u9fa5)]it
二、實例源碼io
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 }
三、實例運行結果
1 過濾出數字:123 2 過濾出字母:ahihdjsadhjwewewe 3 過濾出中文:張三李四 4 過濾出字母、數字和中文:張三34584yuojk李四