Java正則表達式過濾漢字
阿新 • • 發佈:2019-01-31
ted epic char targe generated rep character 智能 ont
作者:itmyhome
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]", "");
}
/**
* 過濾數字
* @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]", "");
}
/**
* 過濾 字母、數字、漢字
* @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你好"));
}
}
作者:itmyhome
再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow
Java正則表達式過濾漢字