package com.kkoolerter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
String str = "我是asdf中国人as ";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("共有 " + count + "个 ");
}
}
这个代码的算法效率不高。有很多改进的地方