码迷,mamicode.com
首页 > 编程语言 > 详细

java中用正則表達式推断中文字符串中是否含有英文或者数字

时间:2014-10-06 23:11:51      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   strong   

bubuko.com,布布扣bubuko.com,布布扣


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中 不表示中文汉字,记住。尽管在正则測试机上能够表示中文汉字

java中用正則表達式推断中文字符串中是否含有英文或者数字

标签:style   blog   http   color   io   os   ar   java   strong   

原文地址:http://www.cnblogs.com/mengfanrong/p/4008737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!