Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the no. of occurrence of the exact word in a file using java

Tags:

java

string

I have requirement in which I have to find the no. of times a particular word appears in a file. For eg.

String str = "Hi hello how are you. hell and heaven. hell, gjh, hello,sdnc ";

Now in this string I want to count no. of times the word "hell" appeared. The count should include "hell" , "hell," all these words but not "hello". So according to the given string I want the count to be 2.

I used following approaches

1st:

int match = StringUtils.countMatches(str, "hell");

StringUtils is of org.apache.commons.lang3 library

2nd:

int count = 0;
Pattern p = Pattern.compile("hell");
                Matcher m = p.matcher(str);
                while (m.find()) {
                    count++;
                }

3rd

int count =0;
String[] s = str.split(" ");
for(String word: s)
if(word.equals("hell")
count++;

the 1st two approaches gave 4 as answer and the 3rd approach gave 1 as answer.

Please suggest anyway in which I can get 2 as answer and fullfill my requirement.

like image 310
Harshita Sethi Avatar asked Dec 20 '22 05:12

Harshita Sethi


2 Answers

You should use word boundary matchers in regex:

Pattern.compile("\\bhell\\b");
like image 98
Evgeniy Dorofeev Avatar answered Dec 29 '22 00:12

Evgeniy Dorofeev


You can use a regular expression with the "\\b" word boundaries as follows:

  int matches = 0;  
  Matcher matcher = Pattern.compile("\\bhell\\b", Pattern.CASE_SENSITIVE).matcher(str);
  while (matcher.find()) matches++;
like image 31
08Dc91wk Avatar answered Dec 29 '22 01:12

08Dc91wk