不匹配给定的字符串
要匹配不包含给定字符串的内容,可以使用否定前瞻:
正则表达式语法:(?!string-to-not-match)
例:
//not matching "popcorn"
String regexString = "^(?!popcorn).*$";
System.out.println("[popcorn] " + ("popcorn".matches(regexString) ? "matched!" : "nope!"));
System.out.println("[unicorn] " + ("unicorn".matches(regexString) ? "matched!" : "nope!"));
输出:
[popcorn] nope!
[unicorn] matched!