Friday, December 27, 2013

Java pattern matching using GLOB patterns (* and ?)

Java has partial glob support since java 7 filePath, but truly glob support I could only find in the good, old, retired, jakarta-oro lib.

Here goes a simple example of how to use it

for(String regex:regexes){
  
    //it's the unix GLOB regex. Java itself does not support it
    //but the good old jakarta oro does
    GlobCompiler compiler=new GlobCompiler();
    Perl5Matcher matcher=new Perl5Matcher();
  
    //there are some interesting options you may want to try
    //org.apache.oro.text.regex.Pattern pat = compiler.compile(regex,GlobCompiler.QUESTION_MATCHES_ZERO_OR_ONE_MASK );
    org.apache.oro.text.regex.Pattern pat = compiler.compile(regex.trim());
    if (site != null && matcher.matches(site, pat)){
        passwordPattern = pattern;
        break;
    }
}

No comments:

Post a Comment