动力节点小编来告诉大家Java正则匹配中文的方法,在Java正则表达式中,一个由汉字和字母数字组合而成的单词看起来就像一个完整的单词。所以匹配单词中的字母数字字符是没有用的。
// 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]
匹配双字节字符(包括汉字在内):[^ -]
匹配空行的正则表达式: [s ?? ]*
匹配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在线学习,里面的课程从入门到精通,由浅到深,通俗易懂,适合没有基础的小伙伴学习,希望对大家能够有所帮助。
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习