在方括号内用^符号表示排除某个字符,使用示例如下:
public static void main(String[] args) {
String regex = "[^a]*"; // 匹配一个不包含字母a的
字符串 boolean hasA = "abcd".matches(regex); // 结果为false
System.out.println(hasA);
hasA = "bcd".matches(regex); // 结果为true
System.out.println(hasA);
}
注意:如果不在方括号内使用表示匹配输入字符串的开始位置。