Java Grep Library

前端 未结 4 2204
南笙
南笙 2020-12-05 23:43

Is there any good, simple Java Grep Library? I\'m not opposed to native code, or scripting, and I\'ll do it, but for my purposes, throughput is not a huge deal, and it would

相关标签:
4条回答
  • 2020-12-06 00:30

    yep. Grep4j - a grep lib for Unix environments, and you can also grep remotely easy : http://code.google.com/p/grep4j/

    0 讨论(0)
  • 2020-12-06 00:33

    String.matches(someRegex); Internally uses java.util.regex.Pattern and Matcher

    0 讨论(0)
  • 2020-12-06 00:41

    Unix4j also implements a (pure java) grep command: 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
    

    Disclaimer: I am one of the contributors to the unix4j project.

    0 讨论(0)
  • 2020-12-06 00:44

    I'm not aware of a sophisticated grep librarystrong text, but you are right: it's not hard to write. I suggest a combination of commons-io and String.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);}
        }
    }
    

    Update Marco pointed out Unix4j which is a quite interesting library which emulates the unix pipelining | with Java method chaining. grep is supported as well as cat, cd, cut, echo, find, grep, head, ls, sed, sort, tail, uniq, wc, barges.

    0 讨论(0)
提交回复
热议问题