更新時間:2022-05-06 10:06:34 來源:動力節(jié)點 瀏覽8751次
動力節(jié)點小編來告訴大家Java正則匹配中文的方法,在Java正則表達式中,一個由漢字和字母數(shù)字組合而成的單詞看起來就像一個完整的單詞。所以\b匹配單詞中的字母數(shù)字字符是沒有用的。
// your code goes here
Pattern with_word_boundary = Pattern.compile("(?i)^\\w+\\b.*$");
Pattern without_word_boundary = Pattern.compile("(?i)^\\w+.*$");
String case_1 = "China中國";
String case_2 = "China 中國";
System.out.println(with_word_boundary.matcher(case_1).matches()); // false
System.out.println(with_word_boundary.matcher(case_2).matches()); // true
System.out.println(without_word_boundary.matcher(case_1).matches()); // true
System.out.println(without_word_boundary.matcher(case_2).matches()); // true
匹配中文字符的正則表達式: [\u4e00-\u9fa5]
匹配雙字節(jié)字符(包括漢字在內(nèi)):[^\x00-\xff]
匹配空行的正則表達式:\n[\s ?? ]*\r
匹配HTML標(biāo)記的正則表達式:/ .* ?? /
匹配首尾空格的正則表達式:(^\s*) ??(\s*$)
用正則表達式限制只能輸入中文:οnkeyup= "value=value.replace(/[^\u4E00-\u9FA5]/g, ' ') " onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\u4E00-\u9FA5]/g, ' ')) "
用正則表達式限制只能輸入全角字符: οnkeyup= "value=value.replace(/[^\uFF00-\uFFFF]/g, ' ') " onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\uFF00-\uFFFF]/g, ' ')) "
以上就是關(guān)于“Java正則匹配中文的方法”介紹,大家如果想了解更多相關(guān)知識,不妨來關(guān)注一下動力節(jié)點的Java在線學(xué)習(xí),里面的課程從入門到精通,由淺到深,通俗易懂,適合沒有基礎(chǔ)的小伙伴學(xué)習(xí),希望對大家能夠有所幫助。
相關(guān)閱讀