首页 > 技术文章 > java中用正则表达式判断中文字符串中是否含有英文或者数字

flybird2014 2014-06-03 17:31 原文


public static boolean includingNUM(String str)throws  Exception{

Pattern p  = Pattern.compile("[\u4e00-\u9fa5]*[\\d|\\w]+[\u4e00-\u9fa5]*");

//或者  Pattern p  = Pattern.compile("[\u4e00-\u9fa5]*[0-9|a-z|A-Z]+[\u4e00-\u9fa5]*");

Matcher M = p.matcher(str);
boolean f =M.matches();
return f;

}


备注: java正则中:

\\D表示非数字
\\d表示数字
\\W表示非单词
\u4e00-\u9fa5是中文字符的编码范围
\\w表示单词
其中一个\是转义字符!!
 [\d\D]、[\w\W]、[\s\S]这三个表示 任何字符。
public static void main(String[] args) { String reg = "[\\d\\D]*\"[^\"]*\"[\\d\\D]*"; String str = "你好\"五都\"辅选";  // 你好"五都"辅选 boolean f = str.matches(reg); System.out.println(f); str = "\"五都\"";// "五都" f = str.matches(reg); System.out.println(f); } 结果均为true;
\\s在java中 不表示中文汉字,记住。虽然在正则测试机上可以表示中文汉字

推荐阅读