日期:2014-05-20 浏览次数:20928 次
public int computeNumOfWord (String word){//查找的单词 this.openReader(); String lineStr = new String(); int countOfWord = 0; Pattern patternOfWord = Pattern.compile("这里要写上正则表达式"); Matcher m; try { while ( (lineStr = this.in.readLine()) != null ){ m = patternOfWord.matcher(lineStr); if (m.matches())//如果匹配 countOfWord++;//则+1 } this.closeReader(); return countOfWord; } catch (IOException e) { e.printStackTrace(); } this.closeReader(); return 0; }
public int computeNumOfWord (String word){//查找的单词 this.openReader(); String lineStr = new String(); int countOfWord = 0; Pattern patternOfWord = Pattern.compile(word); //就是查找的单词 Matcher m; try { while ( (lineStr = this.in.readLine()) != null ){ m = patternOfWord.matcher(lineStr); //if (m.matches())//如果匹配 while (m.find()) //如果能找到匹配 countOfWord++;//则+1 } this.closeReader(); return countOfWord; } catch (IOException e) { e.printStackTrace(); } this.closeReader(); return 0; }
------解决方案--------------------
Pattern patternOfWord = Pattern.compile("这里要写上正则表达式");
写上单词就可以,两边加个空格。
if (m.matches())//如果匹配
改为if (m.find())