3

I want to find and count all the occurrences of the words unit, device, method, module in every line of the text file separately. That's what I've done, but I don't know how to use multiple patterns and how to count the occurrence of every word in the line separately? Now it counts only occurrences of all words together for every line. Thank you in advance!

private void countPaterns() throws IOException {

    Pattern nom = Pattern.compile("unit|device|method|module|material|process|system");

    String str = null;      

    BufferedReader r = new BufferedReader(new FileReader("D:/test/test1.txt")); 

    while ((str = r.readLine()) != null) {
        Matcher matcher = nom.matcher(str);

        int countnomen = 0;
        while (matcher.find()) {
            countnomen++;
        }

        //intList.add(countnomen);
        System.out.println(countnomen + " davon ist das Wort System");
    }
    r.close();
    //return intList;
}
1
  • Please add useful tags, such as what language you're using. "count", "matcher", and "find-occurences" are not useful for this question. Commented Oct 29, 2015 at 20:32

2 Answers 2

3

Better to use a word boundary and use a map to keep counts of each matched keyword.

Pattern nom = Pattern.compile("\\b(unit|device|method|module|material|process|system)\\b");

String str = null;
BufferedReader r = new BufferedReader(new FileReader("D:/test/test1.txt"));
Map<String, Integer> counts = new HashMap<>();

while ((str = r.readLine()) != null) {
    Matcher matcher = nom.matcher(str);

    while (matcher.find()) {
        String key = matcher.group(1);
        int c = 0;
        if (counts.containsKey(key))
            c = counts.get(key);
        counts.put(key, c+1)
    }
}
r.close();

System.out.println(counts);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! it works this way very good, but the only problem that I get the number of occurrences for the whole txt file. But I need to get this number for every new line of the text separately. Before your change it has worked for every line
If you want count per line then add counts.clear() after Matcher matcher = nom.matcher(str); line and make sure to print it inside the outer while loop.
0

Here's a Java 9 (and above) solution:

public static void main(String[] args) {
    List<String> expressions = List.of("(good)", "(bad)");
    String phrase = " good  bad     bad good   good bad bad bad";

    for (String regex : expressions) {
        Pattern gPattern = Pattern.compile(regex);
        Matcher matcher = gPattern.matcher(phrase);
        long count = matcher.results().count();
        System.out.println("Pattern \"" + regex + "\" appears " + count + (count == 1 ? " time" : " times"));
    }
}

Outputs

Pattern "(good)" appears 3 times
Pattern "(bad)" appears 5 times

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.