`

Javaz统计中英文个数__解决技巧[20070924]

    博客分类:
  • JAVA
阅读更多
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//中英文都可以的正则表达式,统计个数!!
public class Reg {

	public int regEx(String str) {
		ArrayList words = new ArrayList();
		Pattern pattern = Pattern.compile("(\\w+)|[\\u4e00-\\u9fa5]+");
		Matcher m = pattern.matcher(str);
		while (m.find()) {
			words.add(m.group());
		}
		System.out.println(words);
		return words.size();
	}

	public static void main(String[] args) {
		Reg r = new Reg();
		System.out.println("个数为:" + r.regEx("索尼wordformatabacka"));
	}

	/***************************************************************************
	 *Getthewordlist. 
	 *@params:需要处理的字符串
	 *@returnCollection:包含单词的List,通过size()方法就能得到字数
	 **************************************************************************/
	public static Collection wordList(String s) {
		Collection temp = new ArrayList();
		Pattern p = Pattern.compile("\\w+");
		Matcher m = p.matcher(s);
		while (m.find()) {
			temp.add(m.group().toString());
		}
		System.out.println("Totalwords:" + temp.size());
		System.out.println("WordListcomplete.");
		return temp;
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics