Java 学习笔记2(一个测试正则表达式的程序)
该程序修改自<Thinking in Java>(the fourth edition)
import java.util.regex.*; public class TestRegularExpression { public static void main(String[] args) { if(args.length < 2) { System.out.println("Usage:\njava TestRegularExpression " + "characterSequence regularExpression+"); System.exit(0); } System.out.println("Input:\""+args[0]+"\""); //for(String arg : args) { for(int i = 1; i < args.length; i++) { System.out.println("RegularExpression : \"" + args[i] + "\""); Pattern p = Pattern.compile(args[i]); Matcher m = p.matcher(args[0]); while(m.find()) { System.out.println("Match \"" + m.group() + "\" at position " + m.start() + "-" + (m.end()-1)); } } } }