import java.util.regex.Pattern;

/**
* Created by Frank
* 在Java中使用正则表达式:测试模式
*/
public class RESimple {
public static void main(String[] args) {
String pattern = "^Q[^u]\\d+\\.";
String[] input = {"QA777. is the next flight. It is on time.", "Quack, Quack, Quack!"};
Pattern p = Pattern.compile(pattern);
for (String s : input) {
/**
* 在字符串中匹配模式,从字符串的首字母开始,或者之前已经成功调用该方法,则从未能符合前面匹配的第一个字符开始
*/
boolean found = p.matcher(s).find();
System.out.println("'" + pattern + "' " + (found ? " matches '" : "doesn't match '") + s + "'");
}
}
}

输出:

'^Q[^u]\d+\.'  matches 'QA777. is the next flight. It is on time.'
'^Q[^u]\d+\.' doesn't match 'Quack, Quack, Quack!'
04-28 15:25