import java.util.HashSet; import java.util.Set; import java.util.regex.*;public class Main { public static void main(String[] args) throws Exception { Pattern p = Pattern.compile("[,.!\\s]+"); Set set = new HashSet(); String[] result = p.split("one,two, three one !mydear four , five"); for (int i=0; i<result.length; i++) set.add(result[i]); // set里面存的字符串就是你要的字符串 } }
masse:Pattern是什么, Pattern p = Pattern.compile("[,.!\\s]+");这句能不能解释下
抱歉,上面那个没有统计出现次数改了一下正则表达式你自己改改,符合自己的要求即可package promtm; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.regex.*;public class Main { public static void main(String[] args) throws Exception { // 你要解析的文章 String s = "one,two, three one !mydear four , five"; // 正则表达式,可以添加你自己的分隔符 Pattern p = Pattern.compile("[,.!;?\\s]+"); String[] result =p.split(s);
Map map = new HashMap(); for (int i=0; i<result.length; i++){ int count = 0; if(map.get(result[i])!=null) count = ((Integer)map.get(result[i])).intValue(); map.put(result[i],new Integer(++count)); }
masse兄弟,你的代码可以改成这样吗, public static void main(String[] args) throws Exception { // 你要解析的文章 String s = "one,two, three one !mydear four , five"; // 正则表达式,可以添加你自己的分隔符 Pattern p = Pattern.compile("[,.!;?\\s]+"); String[] result =p.split(s);
Map map = new HashMap(); for (int i=0; i<result.length; i++){ int count = 0; if(map.get(result[i])!=null) count = ((Integer)map.get(result[i])).intValue(); map.put(result[i],new Integer(++count)); } List list = new ArrayList(); list.addAll( map.keySet()); for(int i=0;i<map.size();i++){ System.out.println((String) list.get(i) + ":" + map.get((String) list.get(i))); } }
用collection去掉重复的,比如HashSet
import java.util.Set;
import java.util.regex.*;public class Main {
public static void main(String[] args) throws Exception {
Pattern p = Pattern.compile("[,.!\\s]+");
Set set = new HashSet();
String[] result =
p.split("one,two, three one !mydear four , five");
for (int i=0; i<result.length; i++)
set.add(result[i]);
// set里面存的字符串就是你要的字符串
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.*;public class Main {
public static void main(String[] args) throws Exception {
// 你要解析的文章
String s = "one,two, three one !mydear four , five";
// 正则表达式,可以添加你自己的分隔符
Pattern p = Pattern.compile("[,.!;?\\s]+");
String[] result =p.split(s);
Map map = new HashMap();
for (int i=0; i<result.length; i++){
int count = 0;
if(map.get(result[i])!=null) count = ((Integer)map.get(result[i])).intValue();
map.put(result[i],new Integer(++count));
}
for (Iterator it=map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry)it.next();
String key = entry.getKey().toString();
int count = ((Integer)entry.getValue()).intValue();
System.out.println(key + ":" + count);
} }
}
三言两语讲不清楚,
最好自己系统的学习一下吧
Pattern.compile("[,.!;?\\s]+");这句话就是确定你所说的分隔符,
上面那句话中,决定了你对文章的分隔符有以下:
逗号,
句点.
感叹号!
问号?
空格\\s其中用[]+表示这些东西可以出现多次比如:
one,,?.two.,.,? three上面这个字符串的单词中间连续出现了多个分隔符,
用我的正则表达式,就能正确分隔出
one
two
three
来
集合有互异性
也就是里面的元素各不相同,
如果不需要统计出现的次数,用集合就可以因为要统计次数,我改成了Map,
map里面存了一系列的数据,
是一系列key和一系列value之间的一一对应
其中key是不会重复的我上面的代码中
key就是单词
value就是出现的次数
http://soulogic.com/doc/RegularExpressions/
public static void main(String[] args) throws Exception {
// 你要解析的文章
String s = "one,two, three one !mydear four , five";
// 正则表达式,可以添加你自己的分隔符
Pattern p = Pattern.compile("[,.!;?\\s]+");
String[] result =p.split(s);
Map map = new HashMap();
for (int i=0; i<result.length; i++){
int count = 0;
if(map.get(result[i])!=null) count = ((Integer)map.get(result[i])).intValue();
map.put(result[i],new Integer(++count));
}
List list = new ArrayList();
list.addAll( map.keySet());
for(int i=0;i<map.size();i++){
System.out.println((String) list.get(i) + ":" + map.get((String) list.get(i)));
}
}