Displays directory listing using regular expressions : Basic Regular Expressions « Regular Expressions « Java






Displays directory listing using regular expressions

Displays directory listing using regular expressions
  
// : c12:DirList.java
// Displays directory listing using regular expressions.
// {Args: "D.*\.java"}
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class DirList {
  public static void main(String[] args) {
    File path = new File(".");
    String[] list;
    if (args.length == 0)
      list = path.list();
    else
      list = path.list(new DirFilter(args[0]));
    Arrays.sort(list, new AlphabeticComparator());
    for (int i = 0; i < list.length; i++)
      System.out.println(list[i]);
  }
}

class DirFilter implements FilenameFilter {
  private Pattern pattern;

  public DirFilter(String regex) {
    pattern = Pattern.compile(regex);
  }

  public boolean accept(File dir, String name) {
    // Strip path information, search for regex:
    return pattern.matcher(new File(name).getName()).matches();
  }
} ///:~

class AlphabeticComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    String s1 = (String) o1;
    String s2 = (String) o2;
    return s1.toLowerCase().compareTo(s2.toLowerCase());
  }
} ///:~



           
         
    
  








Related examples in the same category

1.Meta-characters to match against certain string boundaries
2.Characters classes specifies a list of possible characters
3.POSIX character classes and Java character classes
4.Character Class Matches
5.Greedy Operator Description
6.Reluctant (Lazy) Operator Description
7.Like Regular Expression Demo in a TextFieldLike Regular Expression Demo in a TextField
8.StringConvenience -- demonstrate java.lang.String convenience routineStringConvenience -- demonstrate java.lang.String convenience routine
9.Split a String into a Java Array of Strings divided by an Regular ExpressionsSplit a String into a Java Array of Strings divided by an Regular Expressions
10.Simple example of using Regular Expressions class.Simple example of using Regular Expressions class.
11.Match the Q[^u] pattern against strings from command line
12.demonstrate Regular Expressions: Match -> group()demonstrate Regular Expressions: Match -> group()
13.Show case control using Regular Expressions class.Show case control using Regular Expressions class.
14.Matcher and Pattern demoMatcher and Pattern demo
15.Matcher and Pattern demo 2Matcher and Pattern demo 2
16.Standalone Swing GUI application for demonstrating Regular expressions.Standalone Swing GUI application for demonstrating Regular expressions.
17.Regular Expressions in Action
18.Match SQL string
19.Display all URLs in a web page by matching a regular expression that describes the HTML tag