零宽断言

package org.zly;

 

public class Main1 {

public static void main(String[] args) {

/** 匹配前面不是456的asd **/

String str = "123asd456asd789asd";

System.out.println(str.replaceAll("(?<!456)asd", ""));

/** 匹配后面不是456的asd **/

String str1 = "asd123asd456asd789";

System.out.println(str1.replaceAll("asd(?!456)", ""));

/** 匹配后面是456的asd **/

String str2 = "asd123asd456asd789";

System.out.println(str2.replaceAll("asd(?=456)", ""));

/** 匹配前面是456的asd **/

String str3 = "123asd456asd789asd";

System.out.println(str3.replaceAll("(?<=456)asd", ""));

}

}


你可能感兴趣的:(断言)