Presents your JAVA E-NEWSLETTER for October 3, 2002 <-------------------------------------------> EXPAND GLOBS IN JAVA While the use of regular expressions has grown in programming, the use of glob expressions has declined. However, the Jakarta Original Reusable Objects (ORO) package contains a GlobCompiler for handling globs. Globbing is the oddly named but well-known DOS/UNIX method for using wildcards such as * to signify many files. For example, d*.bat refers to all batch files starting with a d, and *virus.exe refers to all executable files with a name ending with virus.exe. The Jakarta ORO implements the globbing functionality by translating the glob language to a regular expression, so using the globbing system is exactly the same as using a regular expression in ORO. Here's an example: import org.apache.oro.text.*; import org.apache.oro.text.regex.*; public class GlobTest { static public void main(String[] args) throws Exception { GlobCompiler compiler = new GlobCompiler(); Pattern pattern = compiler.compile("*.exe"); PatternMatcher matcher = new Perl5Matcher(); if(matcher.matches("foo.exe", pattern)) { System.err.println("foo.exe matches"); } if(matcher.matches("exe.bar", pattern)) { System.err.println("exe.bar"); } } } The PatternMatcher used is a Perl5Matcher because the glob pattern is translated to Perl 5 syntax. The glob happily matches foo.exe, but it won't match exe.bar, so the only output is the string foo.exe matches. Globbing is a nice feature to add to a user interface, especially if it's a command-line interface. Few users are ready to enter regular expressions, but many are happy to deal with simple globs. ----------------------------------------