计算文件中特定String的出现次数

时间:2012-03-13 18:26:19

标签: java regex string

以下是我所使用的代码:

while ((lineContents = tempFileReader.readLine()) != null)
{
            String lineByLine = lineContents.replaceAll("/\\.", System.getProperty("line.separator")); //for matching /. and replacing it by new line
            changer.write(lineByLine);
            Pattern pattern = Pattern.compile("\\r?\\n"); //Find new line
            Matcher matcher = pattern.matcher(lineByLine);
            while(matcher.find())
            {
                Pattern tagFinder = Pattern.compile("word"); //Finding the word required
                Matcher tagMatcher = tagFinder.matcher(lineByLine);
                while(tagMatcher.find())
                {
                    score++;
                }
                scoreTracker.add(score);
                    score = 0;
            }   
}

我的示例输入包含6行,word的出现为[0,1,0,3,0,0] 所以当我打印scoreTracker(这是ArrayList)时,我想要上面的输出。 但相反,我得[4,4,4,4,4,4],它word的总出现次数,但不是逐行的。{{1}}。 请帮助。

5 个答案:

答案 0 :(得分:3)

lineByLine指向文件的全部内容。这就是你获得[4,4,4,4,4,4]的原因。您需要将每一行存储在另一个变量line中,然后使用tagFinder.find(line)。 最终代码将如下所示

while ((lineContents = tempFileReader.readLine()) != null)
{
    String lineByLine = lineContents.replaceAll("/\\.", System.getProperty("line.separator")); //for matching /. and replacing it by new line
    changer.write(lineByLine);
    Pattern pattern = Pattern.compile(".*\\r?\\n"); //Find new line
    Matcher matcher = pattern.matcher(lineByLine);
    while(matcher.find())
    {
        Pattern tagFinder = Pattern.compile("word"); //Finding the word required
        //matcher.group() returns the input subsequence matched by the previous match.
        Matcher tagMatcher = tagFinder.matcher(matcher.group());
        while(tagMatcher.find())
        {
            score++;
        }
        scoreTracker.add(score);
            score = 0;
    }   
}

答案 1 :(得分:1)

也许这段代码可以帮到你:

    String str = "word word\n \n word word\n \n word\n";
    Pattern pattern = Pattern.compile("(.*)\\r?\\n"); //Find new line
    Matcher matcher = pattern.matcher(str);
    while(matcher.find())
    {
        Pattern tagFinder = Pattern.compile("word"); //Finding the word required
        Matcher tagMatcher = tagFinder.matcher(matcher.group());
        int score = 0;
        while(tagMatcher.find())
        {
            score++;
        }
        System.out.print(score + " ");
    }

输出为2 0 2 0 1它没有高度优化,但问题是你从不限制内部匹配,它总是扫描整行。

答案 2 :(得分:1)

这是因为每次搜索相同的字符串(lineByLine)。您可能想要的是分别搜索每一行。我建议你这样做:

    Pattern tagFinder = Pattern.compile("word"); //Finding the word required
    for(String line : lineByLine.split("\\n")
    {
        Matcher tagMatcher = tagFinder.matcher(line);
        while(tagMatcher.find())
            score++;
        scoreTracker.add(score);
        score = 0;
    }

答案 3 :(得分:1)

原始代码使用tempFileReader.readLine()一次读取一行输入,然后使用matcher查找每行中的行尾。由于lineContents只包含一行,matcher永远不会找到新行,因此会跳过其余代码。 为什么需要两个不同的代码来将输入分成行? 您可以删除与查找新行相关的一些代码。 E.g。

while ((lineContents = tempFileReader.readLine()) != null)
{
      Pattern tagFinder = Pattern.compile("word"); //Finding the word required
      Matcher tagMatcher = tagFinder.matcher(lineContents);
      while(tagMatcher.find())
      {
          score++;
      }
      scoreTracker.add(score);
      score = 0;

}

我在Windows上使用BufferedReader读取的文件test.txt尝试了上面的代码。 E.g。

BufferedReader tempFileReader = new BufferedReader(new FileReader("c:\\test\\test.txt"));

scoreTracker包含[0,1,0,3,0,0]的文件,其中包含您描述的内容。 如果示例输入是所描述的实际文件且tempFileReaderBufferedReader,我不明白你是如何从原始代码中获得[4,4,4,4,4,4]的。查看用于设置tempFileReader

的代码会很有用

答案 4 :(得分:0)

您可以使用Scanner类。您将扫描仪初始化为您想要计算的字符串,然后计算扫描仪找到的这些令牌的数量。

您可以使用FileInputStream直接初始化Scanner。

结果代码只有9行:

File file = new File(fileName);
Scanner scanner = new Scanner(file);
scanner.useDelimiter("your text here");
int occurences;
while(scanner.hasNext()){
     scanner.next();
     occurences++;
}
scanner.close();
相关问题