正则表达式取反
取反正则:^((?!要取反的正则表达式).)*$
示例:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { // 匹配以test开头的字符串 Pattern pattern = Pattern.compile("^test"); Matcher matcher = pattern.matcher("test11"); System.out.println(matcher.find()); // 匹配不是以test开头的字符串 Pattern pattern2 = Pattern.compile("^((?!^test).)*$"); Matcher matcher2 = pattern2.matcher("hellotest"); System.out.println(matcher2.find()); } }