java 正则表达式查找某段字符串中所有小写字母开头的单词并统计次数,按出现次数排序
16lz
2021-01-22
importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.LinkedHashMap;importjava.util.Map;importjava.util.Map.Entry;importjava.util.concurrent.ConcurrentHashMap;importjava.util.regex.Matcher;importjava.util.regex.Pattern;
publicclassGroups{staticpublicfinalStringPOEM="Twasbrilling,andtheslithytoves\n"+"Didgyreandgimbleinthewabe.\n"+"Allmimsyweretheborogoves,\n"+"Andthemomerathsoutgrabe.\n\n"+"BewaretheJabberwock,mysin,\n"+"Thejawsthatbite,theclawsthatcatch.\n"+"BewaretheJubjubbird,andshun\n"+"ThefrumiousBandersnatch.";publicstaticvoidmain(String[]args){Matcherm=//Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(POEM);//取后三个单词//Pattern.compile("\\b[A-Z]\\w+").matcher(POEM);//所有大写单词//Pattern.compile("\\b\\p{Upper}\\w+").matcher(POEM);//所有大写单词//Pattern.compile("\\b\\p{Lower}\\w+").matcher(POEM);//所有小写单词Pattern.compile("\\b[a-z]\\w+").matcher(POEM);//所有小写单词Map<String,Integer>strMap=newConcurrentHashMap<String,Integer>();while(m.find()){if(strMap.get(m.group())==null)strMap.put(m.group(),1);elsestrMap.put(m.group(),strMap.get(m.group())+1);}strMap=sortMap(strMap);for(Stringdanci:strMap.keySet())System.out.println(danci+"="+strMap.get(danci));}publicstaticMapsortMap(MapoldMap){ArrayList<Map.Entry<Character,Integer>>list=newArrayList<Map.Entry<Character,Integer>>(oldMap.entrySet());Collections.sort(list,newComparator<Map.Entry<Character,Integer>>(){publicintcompare(Entry<java.lang.Character,Integer>arg0,Entry<java.lang.Character,Integer>arg1){returnarg1.getValue()-arg0.getValue();}});MapnewMap=newLinkedHashMap();for(inti=0;i<list.size();i++){newMap.put(list.get(i).getKey(),list.get(i).getValue());}returnnewMap;}}运行结果: the=7and=3that=2slithy=1bite=1in=1frumious=1catch=1mome=1sin=1mimsy=1my=1bird=1toves=1shun=1raths=1jaws=1wabe=1brilling=1were=1outgrabe=1gimble=1borogoves=1gyre=1claws=1
publicclassGroups{staticpublicfinalStringPOEM="Twasbrilling,andtheslithytoves\n"+"Didgyreandgimbleinthewabe.\n"+"Allmimsyweretheborogoves,\n"+"Andthemomerathsoutgrabe.\n\n"+"BewaretheJabberwock,mysin,\n"+"Thejawsthatbite,theclawsthatcatch.\n"+"BewaretheJubjubbird,andshun\n"+"ThefrumiousBandersnatch.";publicstaticvoidmain(String[]args){Matcherm=//Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(POEM);//取后三个单词//Pattern.compile("\\b[A-Z]\\w+").matcher(POEM);//所有大写单词//Pattern.compile("\\b\\p{Upper}\\w+").matcher(POEM);//所有大写单词//Pattern.compile("\\b\\p{Lower}\\w+").matcher(POEM);//所有小写单词Pattern.compile("\\b[a-z]\\w+").matcher(POEM);//所有小写单词Map<String,Integer>strMap=newConcurrentHashMap<String,Integer>();while(m.find()){if(strMap.get(m.group())==null)strMap.put(m.group(),1);elsestrMap.put(m.group(),strMap.get(m.group())+1);}strMap=sortMap(strMap);for(Stringdanci:strMap.keySet())System.out.println(danci+"="+strMap.get(danci));}publicstaticMapsortMap(MapoldMap){ArrayList<Map.Entry<Character,Integer>>list=newArrayList<Map.Entry<Character,Integer>>(oldMap.entrySet());Collections.sort(list,newComparator<Map.Entry<Character,Integer>>(){publicintcompare(Entry<java.lang.Character,Integer>arg0,Entry<java.lang.Character,Integer>arg1){returnarg1.getValue()-arg0.getValue();}});MapnewMap=newLinkedHashMap();for(inti=0;i<list.size();i++){newMap.put(list.get(i).getKey(),list.get(i).getValue());}returnnewMap;}}运行结果: the=7and=3that=2slithy=1bite=1in=1frumious=1catch=1mome=1sin=1mimsy=1my=1bird=1toves=1shun=1raths=1jaws=1wabe=1brilling=1were=1outgrabe=1gimble=1borogoves=1gyre=1claws=1
更多相关文章
- 字体图标的引入和通过媒体查询改变导航样式
- HTML样式和常用选择器
- 字体图标的引用和自定义样式/媒体查询的使用
- 数据库的CURD操作、PDO本质与原理的学习
- CSS之伪类选择器和简单盒子简单案例
- 伪类选择器与盒模型常用属性
- 伪类选择器-结构伪类、根据位置选择匹配
- 7.4——常用标签与应用场景之表格与单元格
- css伪类选择器和盒模型