您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Java怎么用正則表達(dá)式過濾出字母、數(shù)字和中文”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Java怎么用正則表達(dá)式過濾出字母、數(shù)字和中文”吧!
1、Java中過濾出字母、數(shù)字和中文的正則表達(dá)式
(1)過濾出字母的正則表達(dá)式
[^(A-Za-z)]
(2) 過濾出 數(shù)字 的正則表達(dá)式
[^(0-9)]
(3) 過濾出 中文 的正則表達(dá)式
[^(\\u4e00-\\u9fa5)]
(4) 過濾出字母、數(shù)字和中文的正則表達(dá)式
[^(a-zA-Z0-9\\u4e00-\\u9fa5)]
2、實例源碼
** * @Title:FilterStr.java * @Package:com.you.dao * @Description:Java中過濾數(shù)字、字母和中文 * @Author: 游海東 * @date: 2014年3月12日 下午7:18:20 * @Version V1.2.3 */ package com.you.dao; /** * @類名:FilterStr * @描述:正則表達(dá)式過濾數(shù)字、字母和中文 * @Author:游海東 * @date: 2014年3月12日 下午7:18:20 */ public class FilterStr { /** * * @Title : filterNumber * @Type : FilterStr * @date : 2014年3月12日 下午7:23:03 * @Description : 過濾出數(shù)字 * @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 : 過濾出字母、數(shù)字和中文 * @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$%$%你好"; /** * 調(diào)用過濾出數(shù)字的方法 */ you = filterNumber(you); /** * 打印結(jié)果 */ System.out.println("過濾出數(shù)字:" + you); /** * 聲明字符串hai */ String hai = "¥%……4556ahihdjsadhj$%$%你好嗎wewewe"; /** * 調(diào)用過濾出字母的方法 */ hai = filterAlphabet(hai); /** * 打印結(jié)果 */ System.out.println("過濾出字母:" + hai); /** * 聲明字符串dong */ String dong = "$%$%$張三34584yuojk李四@#¥#%%¥……%&"; /** * 調(diào)用過濾出中文的方法 */ dong = filterChinese(dong); /** * 打印結(jié)果 */ System.out.println("過濾出中文:" + dong); /** * 聲明字符串str */ String str = "$%$%$張三34584yuojk李四@#¥#%%¥……%&"; /** * 調(diào)用過濾出字母、數(shù)字和中文的方法 */ str = filter(str); /** * 打印結(jié)果 */ System.out.println("過濾出字母、數(shù)字和中文:" + str); } }
3、實例運行結(jié)果
過濾出數(shù)字:123
過濾出字母:ahihdjsadhjwewewe
過濾出中文:張三李四
過濾出字母、數(shù)字和中文:張三34584yuojk李四
ps:Java正則表達(dá)式過濾漢字
String str = "hello你好嗎,我很好 thank you"; String reg = "[\u2E80-\u9FFF]"; Pattern pat = Pattern.compile(reg); Matcher mat = pat.matcher(str); String repickStr = mat.replaceAll(""); System.out.println("過濾中文后: "+repickStr); Demo: import java.util.regex.Matcher; import java.util.regex.Pattern; public class T { /** * 過濾字母 * @param alphabet * @return */ public static String filterAlphabet(String alphabet){ return alphabet.replaceAll("[A-Za-z]", ""); } /** * 過濾數(shù)字 * @param digital * @return */ public static String filterDigital(String digital){ return digital.replaceAll("[0-9]", ""); } /** * 過濾漢字 * @param chin * @return */ public static String filterChinese(String chin){ return chin.replaceAll("[\\u4e00-\\u9fa5]", ""); } /** * 過濾 字母、數(shù)字、漢字 * @param character * @return */ public static String filterAll(String character){ return character.replaceAll("[a-zA-Z0-9\\u4e00-\\u9fa5]", ""); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str = "hello你好嗎,我很好 thank you"; String reg = "[\u2E80-\u9FFF]"; Pattern pat = Pattern.compile(reg); Matcher mat = pat.matcher(str); String repickStr = mat.replaceAll(""); System.out.println("過濾中文后: "+repickStr); System.out.println("-----------------"); System.out.println(filterAlphabet("123abc你好")); System.out.println(filterDigital("123abc你好")); System.out.println(filterChinese("123abc你好")); System.out.println(filterAll("123abc你好")); } }
到此,相信大家對“Java怎么用正則表達(dá)式過濾出字母、數(shù)字和中文”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。