Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Look for a certain String inside another and count how many times it appears

I am trying to search for a String inside a file content which I got into a String.

I've tried to use Pattern and Matcher, which worked for this case:

Pattern p = Pattern.compile("(</machine>)");
Matcher m = p.matcher(text);
while(m.find()) //if the text "(</machine>)" was found, enter
{
    Counter++;
}

return Counter;

Then, I tried to use the same code to find how many tags I have:

Pattern tagsP = Pattern.compile("(</");
Matcher tagsM = tagsP.matcher(text);
while(tagsM.find()) //if the text "(</" was found, enter
{
    CounterTags++;
}

return CounterTags;

which in this case, the return value was always 0.

like image 790
Almog Einstein Avatar asked Jul 19 '15 09:07

Almog Einstein


People also ask

How do you find out how many times something appears in a string?

The count() method can count the number of occurrences of a substring within a larger string. The Python string method count() searches through a string. It returns a value equal to the number of times a substring appears in the string.

How many times a string appears in another string?

count() One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string . count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.

How do you find the occurrence of a string in another string?

Using the indexOf() method The indexOf() method in java is a specialized function to find the index of the first occurrence of a substring in a string.

How do you count specific occurrences of characters in a string?

Given a string and a character, the task is to make a function which count occurrence of the given character in the string. Examples: Input : str = "geeksforgeeks" c = 'e' Output : 4 'e' appears four times in str. Input : str = "abccdefgaa" c = 'a' Output : 3 'a' appears three times in str.


2 Answers

Try using the below code , btw not using Pattern:-

String actualString = "hello hi how(</machine>) are you doing. Again hi (</machine>) friend (</machine>) hope you are (</machine>)doing good.";
//actualString which you get from file content
String toMatch = Pattern.quote("(</machine>)");// for coverting to regex literal
int count = actualString .split(toMatch, -1).length - 1; // split the actualString to array based on toMatch , so final match count should be -1 than array length.
System.out.println(count);

Output :- 4

like image 128
AnkeyNigam Avatar answered Oct 17 '22 00:10

AnkeyNigam


You can use Apache commons-lang util library, there is a function countMatches exactly for you:

int count = StringUtils.countMatches(text, "substring");

Also this function is null-safe. I recommend you to explore Apache commons libraries, they provide a lot of useful common util methods.

like image 31
Gondy Avatar answered Oct 17 '22 00:10

Gondy