日期:2014-05-20 浏览次数:20921 次
/**
* @param args
*/
public static void main(String[] args) {
String str = "我们/n 推动/v 中国/ns 特色/n 世界/n 城市/n 建设/vn";
str = str.replaceAll("(/n\\s+)(?=[^/]+/n\\s+)", "");
System.out.println("new str: " + str);
//然后下面就可以按楼主自己的方法。
Pattern p = Pattern.compile("\\s*([^\\s]+)/n\\s");
Matcher matcher = p.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));//获取被匹配的部分
}
}
------解决方案--------------------
替换两次就可以了
public static void main(String[] args) throws Exception {
String s = "我们/n 推动/v 中国/ns 特色/n 世界/n 城市/n 建设/n";
System.out.println(s);
s = s.replaceAll("\\s*(\\S+?)([^(/n)]$|[^(/n)]\\s+)+", "");
System.out.println(s);
s = s.replaceAll("/n\\s*", "");
System.out.println(s);
}