专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java正则匹配中文的方法

Java正则匹配中文的方法

更新时间:2022-05-06 10:06:34 来源:赢咖4 浏览7939次

赢咖4小编来告诉大家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正则匹配中文的方法”介绍,大家如果想了解更多相关知识,不妨来关注一下赢咖4的Java赢咖4在线学习,里面的课程从入门到精通,由浅到深,通俗易懂,适合没有基础的小伙伴学习,希望对大家能够有所帮助。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>