java字符串利用正则表达式分割

时间:2020-12-15 18:48:38

平时用到,整理的,总感觉缺点什么:

private String getKeywordByContent(String content) {
		StringBuffer sbAllKwyword = new StringBuffer();
		content = content.replaceAll(" ", "");//去掉所有空格
		if(content.length()>10){
			String regex = "[\\u4E00-\u9FA5\\s\\d\\w]{10}";
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(content);
			int i=0;
			while (m.find()&&i<10) {
				sbAllKwyword.append(m.group());
				sbAllKwyword.append("^");		
				i++;
			}
			logger.info("sbAllKwyword={}",sbAllKwyword.toString());
			if(sbAllKwyword.length()>1){				
				sbAllKwyword.deleteCharAt(sbAllKwyword.length() - 1);
			}
		}else{
			sbAllKwyword.append(content);
		}
		return sbAllKwyword.toString();
	}

 需要补充的欢迎随时指出.