正则表达式
中文-正则表达式
[\u4e00-\u9fa5]
[\u4e00-\u9fa5]只匹配一个中文
[\u4e00-\u9fa5]+ 至少匹配一个中文
[^\x00-\xff]
[^\x00-\xff]这个匹配所有非ASCII1的字符,也就是一般意义上的半角字符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();
这篇博客介绍了如何在Java中使用正则表达式匹配中文字符。重点讲述了[u4e00-u9fa5]和[^x00-xff]这两个正则表达式,前者用于匹配单个中文字符,后者匹配所有非ASCII字符。同时提到了Pattern和Matcher的使用。
533

被折叠的 条评论
为什么被折叠?



