Java Find word in a String

后端 未结 5 1234
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 10:25

I need to find a word in a HTML source code. Also I need to count occurrence. I am trying to use regular expression. But it says 0 match found.

I am using regular ex

5条回答
  •  庸人自扰
    2021-01-15 11:18

    You should try this.

    private int getWordCount(String word,String source){
            int count = 0;
            {
                Pattern p = Pattern.compile(word);
                Matcher m = p.matcher(source);
                while(m.find()) count++;
            }
            return count;
        }
    

    Pass the word (Not pattern) you want to search in a string.

提交回复
热议问题