正则表达式
中文-正则表达式
[\u4e00-\u9fa5]
[\u4e00-\u9fa5]只匹配一个中文
[\u4e00-\u9fa5]+ 至少匹配一个中文
[^\x00-\xff]
[^\x00-\xff]这个匹配所有非ASCII
1的字符,也就是一般意义上的半角字符
2
使用
Pattern和Matcher3
// [^0-9]匹配的是任何不在0到9范围内的字符串
// java.util.regex下的Pattern和Matcher
// 从字符串中获取 正则表达式
// 第一种方式
String regEx = "[\u4e00-\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher("正则ZIHWB2019100表达式");
String strings2;
while (m.find()) {
strings2 = m.group().trim();
}
// 第二种方式,反着来,正则为非中文
String regEx = "[^\u4e00-\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher("正则ZIHWB2019100表达式");
String strings2 = m.replaceAll("").trim();
// 不使用Pattern和Matcher,正则为非中文
String regEx = "[^\u4e00-\u9fa5]";
String string = "正则ZIHWB2019100表达式";
String strings2 = string.replaceAll(regEx,"").trim();