取反正则:^((?!要取反的正则表达式).)*$

示例:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {

        // 匹配以test开头的字符串
        Pattern pattern = Patternpile("^test");
        Matcher matcher = pattern.matcher("test11");
        System.out.println(matcher.find());

        // 匹配不是以test开头的字符串
        Pattern pattern2 = Patternpile("^((?!^test).)*$");
        Matcher matcher2 = pattern2.matcher("hellotest");
        System.out.println(matcher2.find());


    }
}

更多推荐

正则表达式取反