Java grep库

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (94)

有什么好的,简单的Java grep库吗?

提问于
用户回答回答于

Unix4j还实现了一个(纯java)grep命令:http//www.unix4j.org

Unix4j.fromStrings("1:A", "2:B", "3:AB", "4:AC", "5:ABC").toFile("myFile.txt");
Unix4j.fromFile("myFile.txt").grep("AB").toStdOut();

>>>
3:AB 
5:ABC
用户回答回答于

我建议的组合commons-ioString.matches(someRegex)

public class Grep extends DirectoryWalker
{
    public Grep(){
        super();
    }

    public List clean(File startDirectory){
      List results = new ArrayList();
      walk(startDirectory, results);
      return results;
    }

    protected boolean handleDirectory(File directory,
                                      int depth, Collection results){
      // Decide if a (sub) directory will be handled for recursive search
      return true;
    }

    protected void handleFile(File file, int depth, Collection results)
    {
        LineIterator it = FileUtils.lineIterator(file, "UTF-8");
        try{
            while (it.hasNext()){
                String line = it.nextLine();
                if(line.matches("myRegEx")){
                    results.add(file);
                }
            }
         }
         finally {LineIterator.closeQuietly(it);}
    }
}

扫码关注云+社区

领取腾讯云代金券