匹配任一字元(包括中英文及任意標點)的Regex Regex : \\w*|\\W*|[\\u4e00-\\u9fa5]這個運算式匹配一個任意中英文字元. 其中: \w : A word character: [a-zA-Z_0-9](匹配一個單詞字元); 其中: \W : A non-word character: [^\w](匹配一個非單詞字元); 其中: [\\u4e00-\\u9fa5] : 匹配一個中文字元;
Ref:http://saltdstar.iteye.com/blog/1041690
Ref:http://www.jb51.net/article/19713.htm
Ref:http://www.jb51.net/article/64301.htm 測試案例:
package cn.mike.march;public class MatchesAllCharactersIncludeChinese { public static void main(String[] args) { String singleWordRegex = "\\w*|\\W*|[\\u4e00-\\u9fa5]"; String chineseCharacter = "犇"; System.out.println("1_: " + chineseCharacter.matches(singleWordRegex)); String multipleWordsRegex = "(\\w*|\\W*|[\\u4e00-\\u9fa5]*)*"; String exampleCharacters = "我的天as牛牪犇df134牛牪犇wsdf64?><?>|_!@@。:;…#$%%^&*(啊.。。"; System.out.println("2_: " + exampleCharacters.matches(multipleWordsRegex)); String regex = "^[A-Z]{1}(\\w*|\\W*|[\\u4e00-\\u9fa5]*)*[。]{1}$"; String exampleChars3 = "B我的天asdf13464?><?>|_!@@#$%%^&*(啊。"; System.out.println("3_: " + exampleChars3.matches(regex)); }}/** Output: 1_: true 2_: true 3_: true*/附錄: 匹配中文字元的Regex: [\u4e00-\u9fa5] 匹配雙位元組字元(包括漢字在內):[^\x00-\xff] 在文字檔裡, 這個運算式可以匹配所有的英文 : /[ -~]/ 這個運算式可以匹配所有的非英文(比如中文) : /[^ -~]/
注意:運算式中間有一個空格;
Regex工具: JavaScriptRegex線上測試載入器:http://tools.jb51.net/regex/javascript Regex線上產生工具 : http://tools.jb51.net/regex/create_reg
by Mike Sun @ 20170330