java正则表达式,如何表示一个非某个字符的匹配

如题所述

方括号内用^符号表示排除某个字符,使用示例如下:

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);

}

注意:如果不在方括号内使用表示匹配输入字符串的开始位置。

温馨提示:答案为网友推荐,仅供参考
相似回答