Search code examples
javadesign-patternsmatcher

Unable to compile pattern matching program for list


I'm trying to build a list and increment a counter for each list item that has a particular keyword. I'm unable to compile this. Why?

        int count = 0;
        String keyword = args[1];

        Pattern p = Pattern.compile(keyword);
        Matcher m = p.matcher(p);

        /* For each paragraph in the document... */
        for (XWPFParagraph paragraph : paragraphs) {
            /* Add to List */
            words.add(paragraph.getText());
            System.out.println(paragraph);

            /* Iterate keyword count */
            while (m.find()) {
                count++;
            }
        }

Solution

  • The problem is probably here

    Matcher m = p.matcher(p);
    

    the argument should be the text to search

    Matcher m = p.matcher(paragraph.getText());