更新時間:2022-05-06 10:06:34 來源:動力節點 瀏覽8636次
動力節點小編來告訴大家Java正則匹配中文的方法,在Java正則表達式中,一個由漢字和字母數字組合而成的單詞看起來就像一個完整的單詞。所以\b匹配單詞中的字母數字字符是沒有用的。
// 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]
匹配雙字節字符(包括漢字在內):[^\x00-\xff]
匹配空行的正則表達式:\n[\s ?? ]*\r
匹配HTML標記的正則表達式:/ .* ?? /
匹配首尾空格的正則表達式:(^\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, ' ')) "
以上就是關于“Java正則匹配中文的方法”介紹,大家如果想了解更多相關知識,不妨來關注一下動力節點的Java在線學習,里面的課程從入門到精通,由淺到深,通俗易懂,適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習