首页 > 代码库 > java中测试正则表达式

java中测试正则表达式

import java.util.regex.*;

public class TestRegularExpression {
  public static void main(String[] args) {
    if (args.length < 2) {
      System.out.println("Usage:\n" + "java TestRegularExpression "
          + "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.compile(args[i]);
      Matcher m = p.matcher(args[0]);
      while (m.find()) {
        System.out.println("Match \"" + m.group() + "\" at positions "
            + m.start() + "-" + (m.end() - 1));
      }
    }
  }
}                                                                                                   

第一个参数为测试字符串

第二个参数为正则表达式



本链接为 红薯  正则表达式30分钟入门教程: http://www.oschina.net/question/12_9507

java中测试正则表达式