正则匹配是否具有中文,数字,英文字符
发布时间:2020-12-14 02:18:39 所属栏目:百科 来源:网络整理
导读:最近项目用到匹配中文,如果匹配中文字符的肯定需要用到ASCII码了,搜了一下,中日韩文的好像是混在一起了,但是中文和中文繁体好像有自己单独的区间。 写了一些测试程序,另外顺便求教, 如何单独只匹配中文?韩文?繁体? 这个目前我还没找到答案,有了会
最近项目用到匹配中文,如果匹配中文字符的肯定需要用到ASCII码了,搜了一下,中日韩文的好像是混在一起了,但是中文和中文繁体好像有自己单独的区间。 写了一些测试程序,另外顺便求教, 如何单独只匹配中文?韩文?繁体? 这个目前我还没找到答案,有了会更新本文的。 public class ZhengZe { public static void main(String[] args) { String str="fff中华中華55nena8a"; System.out.println(haveChinese(str)); System.out.println(haveEnglish(str)); System.out.println(haveNumber(str)); } /** * 匹配是否有汉字(包括繁体的) */ public static boolean haveChinese(String str){ String regex=".*?[u4E00-u9FA5]+.*?"; return Pattern.compile(regex).matcher(str).matches(); } /** * 匹配是否有英文 */ public static boolean haveEnglish(String str){ String regex=".*?[a-zA-Z]+.*?"; return Pattern.compile(regex).matcher(str).matches(); } /** * 匹配是否有数字 */ public static boolean haveNumber(String str){ String regex=".*?d+.*?"; return Pattern.compile(regex).matcher(str).matches(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |