匹配任一字元(包括中英文及中英文標點)的Regex

來源:互聯網
上載者:User
匹配任一字元(包括中英文及任意標點)的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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.