Java Tutorial/Development/Pattern

Материал из Java эксперт
Перейти к: навигация, поиск

Displays directory listing using regular expressions

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;
public class MainClass {
  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);
    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.rupile(regex);
  }
  public boolean accept(File dir, String name) {
    return pattern.matcher(new File(name).getName()).matches();
  }
}





Pattern.compile("[frb][aiu][gx]).matcher("fix the rug with bags")

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Matcher m = Pattern.rupile("[frb][aiu][gx]").matcher("fix the rug with bags");
    while (m.find())
      System.out.println(m.group());
    m.reset("fix the rig with rags");
    while (m.find())
      System.out.println(m.group());
  }
}
/*
*/



fix
rug
bag
fix
rig
rag


Pattern.compile(^java, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String[] args) {
    Pattern p = Pattern.rupile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    Matcher m = p.matcher("java has regex\nJava has regex\n"
        + "JAVA has pretty good regular expressions\n" + "Regular expressions are in Java");
    while (m.find())
      System.out.println(m.group());
  }
}
/*
 */



java
 Java
 JAVA


Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  static public final String poem = "PC Bird Cow Slow Doe Hole Joe\n"
      + "Shy Shift Sleep Do Down Doing.\n" + "Yes No How Whose\n";
  public static void main(String[] args) {
    Matcher m = Pattern.rupile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(poem);
    while (m.find()) {
      for (int j = 0; j <= m.groupCount(); j++)
        System.out.print("[" + m.group(j) + "]");
      System.out.println();
    }
  }
}
/**/



[Doe Hole Joe][Doe][Hole Joe][Hole][Joe]
[Do Down Doing.][Do][Down Doing.][Down][Doing.]
[No How Whose][No][How Whose][How][Whose]


Pattern.compile("!!").split

import java.util.Arrays;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String[] args) {
    String input = "This!!unusual use!!of exclamation!!points";
    System.out.println(Arrays.asList(Pattern.rupile("!!").split(input)));

    System.out.println(Arrays.asList(Pattern.rupile("!!").split(input, 3)));
    System.out.println(Arrays.asList("Aha! String has a split() built in!".split(" ")));
  }
}
/**/



[This, unusual use, of exclamation, points]
[This, unusual use, of exclamation!!points]
[Aha!, String, has, a, split(), built, in!]


Try out regular expressions

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String[] args) {
    if (args.length < 2) {
      System.out.println("Usage:\n" + "java MainClass " + "characterSequence regularExpression+");
      System.exit(0);
    }
    System.out.println("Input: \"" + args[0] + "\"");
    for (int i = 1; i < args.length; i++) {
      System.out.println("Regular expression: \"" + args[i] + "\"");
      Pattern p = Pattern.rupile(args[i]);
      Matcher m = p.matcher(args[0]);
      while (m.find()) {
        System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-"
            + (m.end() - 1));
      }
    }
  }
}